数据清洗之 csv文件读写

csv文件读写

  • pandas内置了10多种数据源读取函数,常见的就是CSV和EXCEL
  • 使用read_csv方式读取。结果为dataframe格式
  • 在读取csv文件时,文件名称尽量是英文
  • 参数较多,可以自行控制,但很多时候用默认参数
  • 读取csv时,注意编码,常用编码为utf-8、gbk、gbk2312和gb18030等
  • 使用to_csv方法快速保存
import numpy as np
import pandas as pd
import os
os.getcwd()
'D:\\Jupyter\\notebook\\Python数据清洗实战\\数据清洗之文件读写'
os.chdir('D:\\Jupyter\\notebook\\Python数据清洗实战\\数据')
baby = pd.read_csv('sam_tianchi_mum_baby.csv', encoding='utf-8')
baby.head(5)
user_id birthday gender
0 2757 20130311 1
1 415971 20121111 0
2 1372572 20120130 1
3 10339332 20110910 0
4 10642245 20130213 0
order = pd.read_csv('meal_order_info.csv', encoding='gbk', dtype={'info_id':str, 'emp_id':str})
order.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 945 entries, 0 to 944
Data columns (total 21 columns):
info_id               945 non-null object
emp_id                945 non-null object
number_consumers      945 non-null int64
mode                  0 non-null float64
dining_table_id       945 non-null int64
dining_table_name     945 non-null int64
expenditure           945 non-null int64
dishes_count          945 non-null int64
accounts_payable      945 non-null int64
use_start_time        945 non-null object
check_closed          0 non-null float64
lock_time             936 non-null object
cashier_id            0 non-null float64
pc_id                 0 non-null float64
order_number          0 non-null float64
org_id                945 non-null int64
print_doc_bill_num    0 non-null float64
lock_table_info       0 non-null float64
order_status          945 non-null int64
phone                 945 non-null int64
name                  945 non-null object
dtypes: float64(7), int64(9), object(5)
memory usage: 155.1+ KB
order.head(5)
info_id emp_id number_consumers mode dining_table_id dining_table_name expenditure dishes_count accounts_payable use_start_time ... lock_time cashier_id pc_id order_number org_id print_doc_bill_num lock_table_info order_status phone name
0 417 1442 4 NaN 1501 1022 165 5 165 2016/8/1 11:05:36 ... 2016/8/1 11:11:46 NaN NaN NaN 330 NaN NaN 1 18688880641 苗宇怡
1 301 1095 3 NaN 1430 1031 321 6 321 2016/8/1 11:15:57 ... 2016/8/1 11:31:55 NaN NaN NaN 328 NaN NaN 1 18688880174 赵颖
2 413 1147 6 NaN 1488 1009 854 15 854 2016/8/1 12:42:52 ... 2016/8/1 12:54:37 NaN NaN NaN 330 NaN NaN 1 18688880276 徐毅凡
3 415 1166 4 NaN 1502 1023 466 10 466 2016/8/1 12:51:38 ... 2016/8/1 13:08:20 NaN NaN NaN 330 NaN NaN 1 18688880231 张大鹏
4 392 1094 10 NaN 1499 1020 704 24 704 2016/8/1 12:58:44 ... 2016/8/1 13:07:16 NaN NaN NaN 330 NaN NaN 1 18688880173 孙熙凯

5 rows × 21 columns

baby = pd.read_csv('baby_trade_history.csv', nrows=100)  # 读取前100行
baby
user_id auction_id cat_id cat1 property buy_mount day
0 786295544 41098319944 50014866 50022520 21458:86755362;13023209:3593274;10984217:21985... 2 20140919
1 532110457 17916191097 50011993 28 21458:11399317;1628862:3251296;21475:137325;16... 1 20131011
2 249013725 21896936223 50012461 50014815 21458:30992;1628665:92012;1628665:3233938;1628... 1 20131011
3 917056007 12515996043 50018831 50014815 21458:15841995;21956:3494076;27000458:59723383... 2 20141023
4 444069173 20487688075 50013636 50008168 21458:30992;13658074:3323064;1628665:3233941;1... 1 20141103
5 152298847 41840167463 121394024 50008168 21458:3408353;13023209:727117752;22009:2741771... 1 20141103
6 513441334 19909384116 50010557 50008168 25935:21991;1628665:29784;22019:34731;22019:20... 1 20121212
7 297411659 13540124907 50010542 50008168 21458:60020529;25935:31381;1633959:27247291;16... 1 20121212
8 82830661 19948600790 50013874 28 21458:11580;21475:137325 1 20121101
9 475046636 10368360710 203527 28 22724:40168;22729:40278;21458:21817;2770200:24... 1 20121101
10 734147966 15307958346 50018202 38 21458:3270827;7361532:28710594;7397093:7536994... 2 20121101
11 68547330 21162876126 50012365 122650008 1628665:3233941;1628665:3233942;1628665:323393... 1 20121123
12 697081418 15898050723 50013636 50008168 21458:19726868;1633959:179425852;13836282:1290... 1 20121123
13 377550424 15771663914 50015841 28 1628665:3233941;1628665:3233942;3914866:11580;... 1 20121123
14 88313935 22532727492 50013711 50008168 1628665:3233941;1628665:3233942;22019:3340598;... 1 20131005
15 25918750 16078389250 50012359 122650008 21458:3405407;1633959:6186201;1628366:32799;81... 1 20131005
16 350288528 35086271572 50010544 50008168 21458:61813;25935:21991;1628665:3233938;162866... 1 20131129
17 348090113 17436967558 50009540 50014815 21458:21910;3110425:30696849;2191928:75373546;... 1 20131129
18 1635282280 36153356431 50013207 50008168 1628665:29784;1628665:29799;2904342:31004;2201... 1 20131129
19 530850018 22058239899 50024147 28 21458:205007542;43307470:5543413;2339128:62147... 1 20140210
20 749507708 19171641742 50018860 28 21458:3602856;1628665:3233941;1628665:3233942;... 1 20140210
21 201088567 38564176352 50013207 50008168 1628665:3233941;1628665:3233942;1628665:323393... 1 20140502
22 469517728 8232924597 211122 38 21458:21782;36786:42781029;13023102:6999219;22... 6 20140502
23 691367866 17712372914 121434042 50014815 21458:49341152;8021059:5525523;6851452:1398669... 1 20140804
24 77193822 35537441586 50006520 50014815 22277:6262384;21458:30992;1628665:3233941;1628... 2 20140804
25 605678021 15502618744 50010555 50008168 25935:31381;1628665:3233941;1628665:3233942;16... 1 20130226
26 47702620 26481508332 121412034 50014815 21458:49341152;11057903:4036007;130475532:7537... 1 20140918
27 763560371 40945285800 50012365 122650008 21458:30992;1628665:3233939;22007:30338;22007:... 1 20150201
28 408028533 35838498718 50012442 50008168 21458:3596449;6811831:3446999;13023209:3446999... 1 20141009
29 53566371 27177784760 121394024 50008168 21458:42090508;1628665:3233941;1628665:3233942... 1 20141009
... ... ... ... ... ... ... ...
70 113473924 15486726090 50014250 28 21458:30015090;1633959:43047819;1627584:28619;... 1 20120905
71 117887031 10956228163 50012451 50008168 1628665:3233942;1628665:3233938;1628665:133527... 1 20120905
72 468447138 15550398428 50012442 50008168 1628665:3233936;1628665:29782;1627349:11462;16... 1 20120905
73 348660284 10896577394 50014250 28 1628665:29796;1628665:108579;1627584:11580;116... 1 20130525
74 129642523 23703880889 50012364 122650008 1628665:3233941;1628665:3233942;1628665:323393... 1 20130525
75 1708761610 18560026026 50016030 50008168 21458:30992;1628665:3233941;25935:31381;22019:... 1 20130525
76 908702885 15515470575 50023591 50022520 21458:26309047;1633959:39224289;11697064:31617... 1 20130312
77 151915451 17305821144 211122 38 21458:21782;36786:42781029;6933553:3313169;130... 2 20140104
78 745002413 36815797313 50023645 28 1628665:82340;21475:11488282;21458:56610575;49... 1 20140104
79 1046234868 10799142007 50023591 50022520 1628665:3233941;1628665:3233942;1628665:323393... 1 20121109
80 810362779 16933071954 50010545 50008168 21458:57430303;1633959:2477;23150:45030;25935:... 1 20121109
81 119784861 20796936076 50140021 50008168 21458:120325094;22019:2026;22019:34731;22019:3... 1 20121129
82 277184180 17734463967 50010555 50008168 21458:3482405;8697758:26247633;1633959:3336498... 1 20121129
83 648623529 16590447919 50010555 50008168 25935:31381;1628665:3233938;1628665:82340;1628... 1 20121129
84 1085938456 39009925227 50013207 50008168 21458:21599;1628665:3233941;22121:30905;122217... 1 20140827
85 2214390386 40856437695 50013636 50008168 21458:216724052;36628385:3480253;1628665:32339... 1 20140827
86 346816172 37132432638 50013636 50008168 21458:216291676;13023209:3583497;35044286:1242... 1 20140827
87 654037597 13775864723 50011993 28 21458:116116655;1633959:3276615;1628862:50276;... 1 20130513
88 1667892062 16767168507 50158020 50008168 1628665:131622;13395135:21671;22019:3340598;22... 1 20130513
89 277279277 18024521052 211122 38 21458:33516;33480:3238774;2653417:7353464;3359... 12 20130513
90 1721792494 36154660054 50008845 28 21458:3400531;5653832:7049425;13023209:7049425... 1 20140312
91 56549058 26930668292 50003700 28 21458:3351431;123273479:31526;1628665:3233941;... 1 20140312
92 696527486 37269469522 50011993 28 21458:118564374;13023209:547499553;122218042:3... 1 20140718
93 643153890 17954181229 50003700 28 123273479:41376163;21475:135183931;1628665:323... 1 20140718
94 362976947 39676108316 50012375 50022520 1628665:29796;1628665:29799;21967:29774;21967:... 1 20140718
95 1097191176 39095838474 50015841 28 1628665:3233941;1628665:3233942;1628665:323393... 1 20150203
96 1107237181 18979330679 121382039 50014815 21458:3371752;13023209:282182273;21475:3779036... 1 20150203
97 1090130969 38473204110 50012364 122650008 21458:30992;1628665:29778;1628665:29799;22007:... 1 20140604
98 373997473 24898348642 50012442 50008168 1628665:29778;22009:29800;122217965:3227750;12... 1 20140604
99 59135448 20494104463 50012375 50022520 21458:7902780;13023209:43969797;2397831:165611... 1 20140929

100 rows × 7 columns

# 数据显示不完全怎么办,可自行设置显示行数和列数
pd.set_option('display.max_columns',20)  # 最多显示20列
pd.set_option('display.max_rows',100)  # 最多显示100行
baby
user_id auction_id cat_id cat1 property buy_mount day
0 786295544 41098319944 50014866 50022520 21458:86755362;13023209:3593274;10984217:21985... 2 20140919
1 532110457 17916191097 50011993 28 21458:11399317;1628862:3251296;21475:137325;16... 1 20131011
2 249013725 21896936223 50012461 50014815 21458:30992;1628665:92012;1628665:3233938;1628... 1 20131011
3 917056007 12515996043 50018831 50014815 21458:15841995;21956:3494076;27000458:59723383... 2 20141023
4 444069173 20487688075 50013636 50008168 21458:30992;13658074:3323064;1628665:3233941;1... 1 20141103
5 152298847 41840167463 121394024 50008168 21458:3408353;13023209:727117752;22009:2741771... 1 20141103
6 513441334 19909384116 50010557 50008168 25935:21991;1628665:29784;22019:34731;22019:20... 1 20121212
7 297411659 13540124907 50010542 50008168 21458:60020529;25935:31381;1633959:27247291;16... 1 20121212
8 82830661 19948600790 50013874 28 21458:11580;21475:137325 1 20121101
9 475046636 10368360710 203527 28 22724:40168;22729:40278;21458:21817;2770200:24... 1 20121101
10 734147966 15307958346 50018202 38 21458:3270827;7361532:28710594;7397093:7536994... 2 20121101
11 68547330 21162876126 50012365 122650008 1628665:3233941;1628665:3233942;1628665:323393... 1 20121123
12 697081418 15898050723 50013636 50008168 21458:19726868;1633959:179425852;13836282:1290... 1 20121123
13 377550424 15771663914 50015841 28 1628665:3233941;1628665:3233942;3914866:11580;... 1 20121123
14 88313935 22532727492 50013711 50008168 1628665:3233941;1628665:3233942;22019:3340598;... 1 20131005
15 25918750 16078389250 50012359 122650008 21458:3405407;1633959:6186201;1628366:32799;81... 1 20131005
16 350288528 35086271572 50010544 50008168 21458:61813;25935:21991;1628665:3233938;162866... 1 20131129
17 348090113 17436967558 50009540 50014815 21458:21910;3110425:30696849;2191928:75373546;... 1 20131129
18 1635282280 36153356431 50013207 50008168 1628665:29784;1628665:29799;2904342:31004;2201... 1 20131129
19 530850018 22058239899 50024147 28 21458:205007542;43307470:5543413;2339128:62147... 1 20140210
20 749507708 19171641742 50018860 28 21458:3602856;1628665:3233941;1628665:3233942;... 1 20140210
21 201088567 38564176352 50013207 50008168 1628665:3233941;1628665:3233942;1628665:323393... 1 20140502
22 469517728 8232924597 211122 38 21458:21782;36786:42781029;13023102:6999219;22... 6 20140502
23 691367866 17712372914 121434042 50014815 21458:49341152;8021059:5525523;6851452:1398669... 1 20140804
24 77193822 35537441586 50006520 50014815 22277:6262384;21458:30992;1628665:3233941;1628... 2 20140804
25 605678021 15502618744 50010555 50008168 25935:31381;1628665:3233941;1628665:3233942;16... 1 20130226
26 47702620 26481508332 121412034 50014815 21458:49341152;11057903:4036007;130475532:7537... 1 20140918
27 763560371 40945285800 50012365 122650008 21458:30992;1628665:3233939;22007:30338;22007:... 1 20150201
28 408028533 35838498718 50012442 50008168 21458:3596449;6811831:3446999;13023209:3446999... 1 20141009
29 53566371 27177784760 121394024 50008168 21458:42090508;1628665:3233941;1628665:3233942... 1 20141009
30 69873877 40133707057 50010555 50008168 21458:30992;25935:31381;1628665:3233941;162866... 1 20141017
31 1609185254 42001753405 121394024 50008168 21458:30992;1628665:3233942;1628665:3233936;16... 1 20141228
32 1746148145 41181827319 50012365 122650008 21458:621749996;13023209:12868;122217803:30916... 1 20141228
33 256475742 39059292616 121452056 50008168 1628665:29784;1628665:29782;122217801:50793479... 1 20140711
34 405194127 15462429573 50007011 50008168 21458:35624651;1633959:7320293;1628665:3233941... 1 20120819
35 938309370 14149079479 50023669 28 21458:4204704;11820090:105550653;11644036:2861... 1 20120819
36 84258337 14653740604 50016704 50022520 21458:3394654;5261331:4377028;1633959:4377028;... 1 20120819
37 14466144 17610665576 50011993 28 21458:104000;21475:137325 1 20130327
38 177724549 14228645401 50018824 38 21475:108284;6933666:96059;33595:16453265;2145... 1 20130327
39 727823869 39674261411 121466023 50008168 21458:14332755;1628665:3233941;1628665:3233942... 2 20140813
40 659020106 40484992676 50011993 28 21458:16162126;13023209:10551667;122218042:605... 1 20140813
41 46277938 40070019945 50006602 50008168 21458:29563;10984217:21985;13023209:3488197;21... 1 20140813
42 827091396 18678458676 50010566 50008168 21458:46906;13023209:158751187;25935:21991;320... 1 20140911
43 18100946 38451267766 121540027 28 21458:215485914;125501489:19689726;11945782:78... 1 20140911
44 725813399 40519533209 50010544 50008168 21458:32270;13023209:669513679;25935:21991;162... 1 20140911
45 1054852159 19063296909 50006235 50008168 1628665:3233941;21475:17106236;21475:17106365;... 2 20140703
46 262519726 19051046285 121398041 28 11666049:40203;21458:3961150;17472269:13302841... 1 20140703
47 87207277 14234909614 121470030 50014815 21458:30992;1628665:3233941;1628665:3233942;16... 1 20140703
48 1053602675 20252281923 50013636 50008168 21458:216724052;1628665:29798;1628665:29796;25... 1 20140220
49 103125167 18426669796 50018438 50014815 21458:46896;1628665:3233941;1628665:3233942;21... 16 20140220
50 886492677 19668429343 50016704 50022520 21458:3662539;5261385:3351834;13023209:3351834... 1 20140628
51 115566151 14778919435 50013187 28 1628665:3233938;1628665:29796;1628665:133527;1... 1 20140113
52 55544814 4917672059 50015727 50014815 21458:4540492;1633959:58840623;7107736:3227806... 4 20131106
53 1714403831 22443564698 50014129 28 21458:57737100;12102318:7282254;11945782:78135... 1 20131106
54 723975586 8096949165 50023591 50022520 1628665:3233941;1628665:29798;1628665:3233938;... 1 20120911
55 66451440 9258781845 50013636 50008168 21458:11580;1628665:3233941;1628665:3233936;16... 1 20120911
56 47342027 14066344263 50013636 50008168 21458:21599;13585028:3416646;1628665:3233942;1... 1 20120911
57 354780072 17851314047 50016704 50022520 21458:3394654;5261331:237777686;1633959:237777... 1 20130725
58 1660751516 12496195786 50024842 50008168 1628665:3233941;25935:21991;13545112:43704;135... 2 20130725
59 1981826945 40793811285 50010538 50008168 21458:37946447;13023209:696649694;25935:21990;... 1 20150108
60 61003275 36738992094 50018831 50014815 21458:21899;7255169:61035386;7368343:7327107;1... 3 20150108
61 848482116 42178787281 50010538 50008168 21458:31340;13023209:25581424;25935:21991;1628... 1 20150119
62 405014302 43130926446 50012777 50014815 21458:46850;1628665:3233939;1628665:92012;1628... 1 20150119
63 806635728 38985185626 121452056 50008168 21458:9398440;1628665:29784;122217801:3265977;... 1 20140615
64 1970876909 20197969079 211122 38 6940834:29865;21458:3270820;1629375:3253542;32... 1 20141017
65 605724983 19747694834 50006520 50014815 21458:30992 12 20141017
66 2148300507 41694440222 50010549 50008168 115931637:36783070;25935:21990;1628665:3233941... 1 20141112
67 818595619 36424612559 50013636 50008168 21458:99466824;13023209:3334185;120198214:3334... 1 20141112
68 442760655 36611607467 50016704 50022520 1628665:3233941;1628665:29790;1628665:3233936;... 1 20141228
69 1026379511 19281156237 50012375 50022520 21458:3731805;1633959:14267607;2397831:1656121... 1 20120905
70 113473924 15486726090 50014250 28 21458:30015090;1633959:43047819;1627584:28619;... 1 20120905
71 117887031 10956228163 50012451 50008168 1628665:3233942;1628665:3233938;1628665:133527... 1 20120905
72 468447138 15550398428 50012442 50008168 1628665:3233936;1628665:29782;1627349:11462;16... 1 20120905
73 348660284 10896577394 50014250 28 1628665:29796;1628665:108579;1627584:11580;116... 1 20130525
74 129642523 23703880889 50012364 122650008 1628665:3233941;1628665:3233942;1628665:323393... 1 20130525
75 1708761610 18560026026 50016030 50008168 21458:30992;1628665:3233941;25935:31381;22019:... 1 20130525
76 908702885 15515470575 50023591 50022520 21458:26309047;1633959:39224289;11697064:31617... 1 20130312
77 151915451 17305821144 211122 38 21458:21782;36786:42781029;6933553:3313169;130... 2 20140104
78 745002413 36815797313 50023645 28 1628665:82340;21475:11488282;21458:56610575;49... 1 20140104
79 1046234868 10799142007 50023591 50022520 1628665:3233941;1628665:3233942;1628665:323393... 1 20121109
80 810362779 16933071954 50010545 50008168 21458:57430303;1633959:2477;23150:45030;25935:... 1 20121109
81 119784861 20796936076 50140021 50008168 21458:120325094;22019:2026;22019:34731;22019:3... 1 20121129
82 277184180 17734463967 50010555 50008168 21458:3482405;8697758:26247633;1633959:3336498... 1 20121129
83 648623529 16590447919 50010555 50008168 25935:31381;1628665:3233938;1628665:82340;1628... 1 20121129
84 1085938456 39009925227 50013207 50008168 21458:21599;1628665:3233941;22121:30905;122217... 1 20140827
85 2214390386 40856437695 50013636 50008168 21458:216724052;36628385:3480253;1628665:32339... 1 20140827
86 346816172 37132432638 50013636 50008168 21458:216291676;13023209:3583497;35044286:1242... 1 20140827
87 654037597 13775864723 50011993 28 21458:116116655;1633959:3276615;1628862:50276;... 1 20130513
88 1667892062 16767168507 50158020 50008168 1628665:131622;13395135:21671;22019:3340598;22... 1 20130513
89 277279277 18024521052 211122 38 21458:33516;33480:3238774;2653417:7353464;3359... 12 20130513
90 1721792494 36154660054 50008845 28 21458:3400531;5653832:7049425;13023209:7049425... 1 20140312
91 56549058 26930668292 50003700 28 21458:3351431;123273479:31526;1628665:3233941;... 1 20140312
92 696527486 37269469522 50011993 28 21458:118564374;13023209:547499553;122218042:3... 1 20140718
93 643153890 17954181229 50003700 28 123273479:41376163;21475:135183931;1628665:323... 1 20140718
94 362976947 39676108316 50012375 50022520 1628665:29796;1628665:29799;21967:29774;21967:... 1 20140718
95 1097191176 39095838474 50015841 28 1628665:3233941;1628665:3233942;1628665:323393... 1 20150203
96 1107237181 18979330679 121382039 50014815 21458:3371752;13023209:282182273;21475:3779036... 1 20150203
97 1090130969 38473204110 50012364 122650008 21458:30992;1628665:29778;1628665:29799;22007:... 1 20140604
98 373997473 24898348642 50012442 50008168 1628665:29778;22009:29800;122217965:3227750;12... 1 20140604
99 59135448 20494104463 50012375 50022520 21458:7902780;13023209:43969797;2397831:165611... 1 20140929
os.getcwd()
'D:\\Jupyter\\notebook\\Python数据清洗实战\\数据'
# 保存csv文件
# index=False 表示索引行不写入csv文件
baby.to_csv('a1.csv', encoding='utf-8', index=False)
发布了248 篇原创文章 · 获赞 293 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/qq_29339467/article/details/105452928