数据清洗是整个数据分析过程的第一步,也是整个数据分析项目中最耗费时间的一步;数据分析的过程决定了数据分析的准确性。
numpy常用数据结构:
numpy中重用的数据结构是ndarray格式
使用array函数创建,语法格式为array(列表或元组)
可以使用其他函数例如arange/linspace/zeros等创建
import numpy as np
#使用array函数创建
arr1 = np.array([-7, 4, 4, 6, 89])
type(arr1)
numpy.ndarray
arr3 = np.array([[1,2,3,4],[5,6,7,8],[9,10,11,12]])#嵌套列表
arr3
array([[ 1, 2, 3, 4],
[ 5, 6, 7, 8],
[ 9, 10, 11, 12]])
np.arange(1,10,0.5)#使用arange函数
array([1. , 1.5, 2. , 2.5, 3. , 3.5, 4. , 4.5, 5. , 5.5, 6. , 6.5, 7. , 7.5, 8. , 8.5, 9. , 9.5])
np.linspace(1,10,20,endpoint = True)#使用等差数列:起始值,终止值,元素个数
array([ 1. , 1.47368421, 1.94736842, 2.42105263, 2.89473684,
3.36842105, 3.84210526, 4.31578947, 4.78947368, 5.26315789,
5.73684211, 6.21052632, 6.68421053, 7.15789474, 7.63157895,
8.10526316, 8.57894737, 9.05263158, 9.52631579, 10. ])
(10-1)/19
0.47368421052631576
np.zeros([4,5])#使用zeros函数
array([[0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0.]])
np.zeros(4)
array([0., 0., 0., 0.])
np.ones([2,3])#使用ones函数
array([[1., 1., 1.],
[1., 1., 1.]])
arr3 + 1#每个元素都加1
array([[ 2, 3, 4, 5],
[ 6, 7, 8, 9],
[10, 11, 12, 13]])
arr3.ndim#数组维度
2
arr1.shape#数组形状
(5,)#一维数组:5个元素
arr3.shape
(3, 4)#二维数组:3行4列
arr3.size#一共有多少元素
12
arr3.dtype#元素类型
dtype('int32')
#使用元组
data2 = ((1.2,2.3,3,4,5.6),(4.5,6.7,34,3.6,5),(2.5,4.6,7.8,8.9,50))#嵌套元组
data2
((1.2, 2.3, 3, 4, 5.6), (4.5, 6.7, 34, 3.6, 5), (2.5, 4.6, 7.8, 8.9, 50))
arr2 = np.array(data2)
arr2
array([[ 1.2, 2.3, 3. , 4. , 5.6],
[ 4.5, 6.7, 34. , 3.6, 5. ],
[ 2.5, 4.6, 7.8, 8.9, 50. ]])
arr2[0]#访问第一个
array([1.2, 2.3, 3. , 4. , 5.6])
arr2[0:3]
array([[ 1.2, 2.3, 3. , 4. , 5.6],
[ 4.5, 6.7, 34. , 3.6, 5. ],
[ 2.5, 4.6, 7.8, 8.9, 50. ]])
arr2[1:3]
array([[ 4.5, 6.7, 34. , 3.6, 5. ],
[ 2.5, 4.6, 7.8, 8.9, 50. ]])
arr2[1,3]#第二行,第四列
3.6
arr2[1][3]#第二行,第四列
3.6
arr2[:,3]#第四列
array([4. , 3.6, 8.9])
arr2[:,1:3]#第二列到第三列
array([[ 2.3, 3. ],
[ 6.7, 34. ],
[ 4.6, 7.8]])
#排序
s = np.array([1,2,4,3,6,5,90,67,54,32,12,9,7])
np.sort(s)#返回的是一个视图
array([ 1, 2, 3, 4, 5, 6, 7, 9, 12, 32, 54, 67, 90])
s
array([ 1, 2, 4, 3, 6, 5, 90, 67, 54, 32, 12, 9, 7])
s = np.sort(s)
s
array([ 1, 2, 3, 4, 5, 6, 7, 9, 12, 32, 54, 67, 90])
sorted(s,reverse=True)
[90, 67, 54, 32, 12, 9, 7, 6, 5, 4, 3, 2, 1]
np.array(sorted(s,reverse=True))
array([90, 67, 54, 32, 12, 9, 7, 6, 5, 4, 3, 2, 1])
np.argsort(s)
array([ 0, 1, 3, 2, 5, 4, 12, 11, 10, 9, 8, 7, 6], dtype=int64)
s
array([ 1, 2, 4, 3, 6, 5, 90, 67, 54, 32, 12, 9, 7])
#二维数组排序
arr1 = np.array([[0,1,3,],[4,2,9],[4,5,9],[1,-3,4]])
np.sort(arr1,axis = 0)#axis=0是沿着行的方向排序;axis=1是沿着列的方向排序
array([[ 0, -3, 3],
[ 1, 1, 4],
[ 4, 2, 9],
[ 4, 5, 9]])
np.sort(arr1,axis = 1)#axis=0是沿着行的方向排序;axis=1是沿着列的方向排序
array([[ 0, 1, 3],
[ 2, 4, 9],
[ 4, 5, 9],
[-3, 1, 4]])
s
array([ 1, 2, 4, 3, 6, 5, 90, 67, 54, 32, 12, 9, 7])
np.where(s>7,1,-1)#大于7返回1,否则返回-1
array([-1, -1, -1, -1, -1, -1, 1, 1, 1, 1, 1, 1, -1])
np.where(s>7,s,-1)
array([-1, -1, -1, -1, -1, -1, 90, 67, 54, 32, 12, 9, -1])
np.where(s>7,1,-4)
array([-4, -4, -4, -4, -4, -4, 1, 1, 1, 1, 1, 1, -4])
np.extract(s>7,s)#筛选出s>7的元素
array([90, 67, 54, 32, 12, 9])
#序列的创建(series)
import pandas as pd
series1 = pd.Series([2.8,3.01,8.99,8.58,5.18])
series1
0 2.80
1 3.01
2 8.99
3 8.58
4 5.18
dtype: float64
type(series1)#序列
pandas.core.series.Series
series2 = pd.Series([2.8,3.01,8.99,8.58,5.18],index=['a','b','c','d','e'],name='这是一个序列')
series2
a 2.80
b 3.01
c 8.99
d 8.58
e 5.18
Name: 这是一个序列, dtype: float64
series3 = pd.Series({'北京':2.8,'上海':3.01,'广东':8.99,'江苏':9.73,'浙江':5.18})#用字典
series3
北京 2.80
上海 3.01
广东 8.99
江苏 9.73
浙江 5.18
dtype: float64
series3[0:3]#左闭右开
北京 2.80
上海 3.01
广东 8.99
dtype: float64
series3['北京':'江苏']#左右都闭
北京 2.80
上海 3.01
广东 8.99
江苏 9.73
dtype: float64
series1.values#输入的是值
array([2.8 , 3.01, 8.99, 8.58, 5.18])
series3.index
Index(['北京', '上海', '广东', '江苏', '浙江'], dtype='object')
series1.index
RangeIndex(start=0, stop=5, step=1)
series1.dtype
dtype('float64')
dataframe
list1 = [['张三',23,'男'],['李四',27,'女'],['王二',26,'女']]#使用嵌套列表
df1 = pd.DataFrame(list1,columns=['姓名','年龄','性别'])
df1.head(5)
姓名 年龄 性别
0 张三 23 男
1 李四 27 女
2 王二 26 女
df2 = pd.DataFrame({'姓名':['张三','李四','王二'],'年龄':[23,24,26],'性别':['男','女','女'],})#使用字典
df2.head(5)
姓名 年龄 性别
0 张三 23 男
1 李四 24 女
2 王二 26 女
array1 = np.array([['张三',23,'男'],['李四',27,'女'],['王二',26,'女']])
df3 = pd.DataFrame(array1,columns=['姓名','年龄','性别'],index=['a','b','c'])#使用数组
df3
姓名 年龄 性别
a 张三 23 男
b 李四 27 女
c 王二 26 女
df3.values
array([['张三', '23', '男'],
['李四', '27', '女'],
['王二', '26', '女']], dtype=object)
df3.index
Index(['a', 'b', 'c'], dtype='object')
df3.columns
Index(['姓名', '年龄', '性别'], dtype='object')
df3.columns.tolist()#转化成列表
['姓名', '年龄', '性别']
df3.ndim
2
series1.ndim
1
df2.shape
(3, 3)
series1.shape
(5,)
df3.size
9
df3.dtypes
姓名 object
年龄 object
性别 object
dtype: object
df2.dtypes
姓名 object
年龄 int64
性别 object
dtype: object
1.pandas内置了10多种数据源读取函数,常见的就是CSV和EXCEL
2.使用read_csv方式读取,结果为dataframe格式
3.在读取csv文件时,文件名称尽量是英文
4.参数较多,可以自行控制,但很多时候用默认参数
5.读取csv时,注意编码,常用编码为utf-8、gbk、gbk2312和gb18030等
6.使用to_csv方法快速保存
import numpy as np
import pandas as pd
import os#用于更改文件路径
os.getcwd()#当前文件路径
'D:\\code\\jupyter\\course'
os.chdir('D:\\code\\jupyter\\course\\代码和数据')#更改文件存放路径
baby = pd.read_csv('sam_tianchi_mum_baby.csv',encoding = 'utf-8')#默认是utf-8。read_csv会把数据的第一行当做表头即‘列索引’,行索引默认从0开始
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')#utf-8报错,可以尝试gbk.
order.info()
RangeIndex: 945 entries, 0 to 944
Data columns (total 21 columns):
info_id 945 non-null int64
emp_id 945 non-null int64
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(11), object(3)
memory usage: 155.1+ KB
order = pd.read_csv('meal_order_info.csv',encoding = 'gbk',dtype = {'info_id':str, 'emp_id':str})#希望把数值型读成字符串类型
order.info()
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(10)
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 381 1243 4 NaN 1487 1008 239 7 239 2016/8/1 13:15:42 ... 2016/8/1 13:23:42 NaN NaN NaN 330 NaN NaN 1 18688880441 沈晓雯
6 429 1452 4 NaN 1501 1022 699 15 699 2016/8/1 13:17:37 ... 2016/8/1 13:34:18 NaN NaN NaN 330 NaN NaN 1 18688880651 苗泽坤
7 433 1109 8 NaN 1490 1011 511 14 511 2016/8/1 13:38:27 ... 2016/8/1 13:50:16 NaN NaN NaN 330 NaN NaN 1 18688880212 李达明
8 569 1143 6 NaN 1488 1009 326 9 326 2016/8/1 17:06:20 ... 2016/8/1 17:18:20 NaN NaN NaN 330 NaN NaN 1 18688880272 陈有浩
9 655 1268 8 NaN 1492 1013 263 10 263 2016/8/1 17:32:27 ... 2016/8/1 17:44:27 NaN NaN NaN 330 NaN NaN 1 18688880466 沈丹丹
10 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
baby.to_csv('al.csv',encoding = 'utf-8',index=False)#保存为utf-8格式,下次读取就得用utf-8. 默认为utf-8,可不写。 索引不写入
1.使用read_excel方法读取,结果为dataframe格式
2.读取Excel文件和csv文件参数大致一样,但要考虑工作表sheet页
3.参数较多,可自行控制,但很多时候用默认参数
4.读取Excel时,注意编码,常用编码为utf-8、gbk、gbk2312和gb18030等
5.使用to_excel快速保存为xlsx格式
df1 = pd.read_excel('meal_order_detail.xlsx',encoding = 'utf-8', sheet_name='meal_order_detail1')
df1.head(10)
detail_id order_id dishes_id logicprn_name parent_class_name dishes_name itemis_add counts amounts cost place_order_time discount_amt discount_reason kick_back add_inprice add_info bar_code picture_file emp_id
0 2956 417 610062 NaN NaN 蒜蓉生蚝 0 1 49 NaN 2016-08-01 11:05:36 NaN NaN NaN 0 NaN NaN caipu/104001.jpg 1442
1 2958 417 609957 NaN NaN 蒙古烤羊腿\r\n\r\n\r\n 0 1 48 NaN 2016-08-01 11:07:07 NaN NaN NaN 0 NaN NaN caipu/202003.jpg 1442
2 2961 417 609950 NaN NaN 大蒜苋菜 0 1 30 NaN 2016-08-01 11:07:40 NaN NaN NaN 0 NaN NaN caipu/303001.jpg 1442
3 2966 417 610038 NaN NaN 芝麻烤紫菜 0 1 25 NaN 2016-08-01 11:11:11 NaN NaN NaN 0 NaN NaN caipu/105002.jpg 1442
4 2968 417 610003 NaN NaN 蒜香包 0 1 13 NaN 2016-08-01 11:11:30 NaN NaN NaN 0 NaN NaN caipu/503002.jpg 1442
5 1899 301 610019 NaN NaN 白斩鸡 0 1 88 NaN 2016-08-01 11:15:57 NaN NaN NaN 0 NaN NaN caipu/204002.jpg 1095
6 1902 301 609991 NaN NaN 香烤牛排\r\n 0 1 55 NaN 2016-08-01 11:19:12 NaN NaN NaN 0 NaN NaN caipu/201001.jpg 1095
7 1906 301 609983 NaN NaN 干锅田鸡 0 1 88 NaN 2016-08-01 11:22:21 NaN NaN NaN 0 NaN NaN caipu/205003.jpg 1095
8 1907 301 609981 NaN NaN 桂圆枸杞鸽子汤 0 1 48 NaN 2016-08-01 11:22:53 NaN NaN NaN 0 NaN NaN caipu/205001.jpg 1095
9 1908 301 610030 NaN NaN 番茄有机花菜 0 1 32 NaN 2016-08-01 11:23:56 NaN NaN NaN 0 NaN NaN caipu/304004.jpg 1095
df1 = pd.read_excel('meal_order_detail.xlsx',encoding = 'utf-8', sheet_name=0)
import os
os.getcwd()
'D:\\code\\jupyter\\course
df1.to_excel('asdf.xlsx',index = False, sheet_name = 'one')#工作簿名称sheet_name
1.使用sqlalchemy建立连接
2.需要知道数据库的相关参数,如数据库IP地址、用户名和密码等
3.通过pandas中read_sql函数读入,读取完以后是dataframe格式
4.通过dataframe的to_sql方法保存
import pandas as pd
import pymysql
from sqlalchemy import create_engine
conn = create_engine('mysql+pymysql://root:123456@localhost:3306/meeting')
sql = 'select * from employee'
df1 = pd.read_sql(sql,con = conn)
df1.head()
employeeid employeename username phone email status departmentid password role
0 8 王晓华 wangxh 13671075406 wang@qq.com 1 1 1 1
1 9 林耀坤 li56 13671075406 yang@qq.com 1 2 1 2
2 10 熊杰文 xiongjw 134555555 xiong@qq.com 1 3 1 2
3 11 王敏 wangmin 1324554321 wangm@qq.com 0 4 1 2
4 12 林耀坤 linyk 1547896765 kun@qq.com 1 7 1 2
def query(table):
host = 'localhost'
user = 'root'
password = '123456'
database = 'meeting'
port = 3306
conn = create_engine('mysql+pymysql://{}:{}@{}:{}/{}'.format(user,password,host,port,database))
sql = 'select * from '+ table
result = pd.read_sql(sql,con = conn)
return result
df2 = query('meetingroom')
df2
roomid roomnum roomname capacity status description
0 5 101 第一会议室 15 0 公共会议室
1 6 102 第二会议室 5 0 管理部门会议室
2 7 103 第三会议室 12 0 市场部专用会议室
3 8 401 第四会议室 15 0 公共会议室
4 9 201 第五会议室 15 0 最大会议室
5 10 601 第六会议室 12 0 需要提前三天预定
import os
os.chdir('D:\code\jupyter\course\代码和数据')
df = pd.read_csv('baby_trade_history.csv')
try:
df.to_sql('testdf',con = conn, index = False, if_exists='replace')
except:
print('error')
1.在数据中,选择需要的行或者列
2.基础索引方式,即直接引用,
3.loc[行索引名称或者条件,列索引名称或者标签]
4.iloc[行索引位置,列索引位置]
5.注意区别loc和iloc
import numpy as np
import pandas as pd
import os
os.chdir('D:\code\jupyter\course\代码和数据')
df = pd.read_csv('baby_trade_history.csv',encoding = 'utf-8',dtype = {'user_id':str})
df
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
... ... ... ... ... ... ... ...
29921 1372572 16915013171 50008845 28 21458:30992;1628665:3233941;1628665:3233942;16... 1 20130327
29922 646265934 10310068684 50012449 50008168 21458:70899896;8539985:3374457;1633959:3374457... 1 20130823
29923 184923046 15265710813 50006602 50008168 30497:46157;30498:46160;1628665:3233941;162866... 1 20130823
29924 167288532 18836766607 50012788 28 2112993:29274;122277833:104377;1628665:3233941... 1 20140913
29925 54855720 39635136808 50018436 50014815 1628665:3233941;1628665:3233942;1628665:323393... 2 20140913
29926 2183330808 19704833222 50011993 28 21458:119679150;13023209:4067094;122218042:216... 1 20140913
29927 372911996 14288242617 50012438 50014815 21458:8535341;1628665:3233941;5970350:3402020;... 1 20121222
29928 22414724 20959068389 50010544 50008168 21458:61834;25935:21990;1628665:3233941;162866... 1 20121222
29929 113446171 37455013590 50023663 28 11760865:6120557;21458:3314841;122277597:2054;... 1 20140831
29930 522850986 18220260582 122678023 28 21458:91312408;138253785:653822445;135925585:1... 1 20140831
29931 671912471 40608095216 50010558 50008168 25935:21991;1628665:3233942;1628665:3233938;12... 1 20140831
29932 2212582093 16684531006 50006095 28 21458:3532516;8088680:4482698;1628665:3233941;... 1 20150205
29933 712130383 35024165075 50010558 50008168 21458:30992;25935:31381;1628665:29799;12221780... 1 20150205
29934 326432534 40599422228 50012428 28 1628665:3233941;1628665:29790;1628665:3233936;... 1 20150110
29935 1663431626 20045810860 50018831 50014815 21458:3630550;7255245:3630549;27174406:1782825... 2 20150110
29936 1041477880 39339180944 50023591 50022520 1628665:29790;1628665:29778;1628665:3233938;16... 1 20140710
29937 2078287226 40289849863 50013207 50008168 21458:100854706;13023209:647423831;22121:11712... 1 20141029
29938 22270319 35760225761 50023613 50022520 NaN 1 20141029
29939 2074613684 40798454555 203319 28 122217795:29277;134943792:29774;134943792:2977... 1 20141108
29940 1048135914 35165371283 121394024 50008168 1628665:3233941;1628665:3233942;1628665:323393... 1 20141108
29941 413188001 16521677358 50012478 50014815 21458:28155;5434803:3636603;2815901:22583732;1... 1 20130107
29942 474062095 21129724585 50013207 50008168 21458:21599;1628665:29798;1628665:3233938;1628... 1 20130107
29943 797710454 18176728510 50013177 28 1628665:3233941;1628665:3233942;1628665:323393... 1 20130107
29944 1716505453 37844041565 50010555 50008168 21458:30992;25935:31381;1628665:3233941;162866... 1 20141231
29945 1966692323 42504930457 50012359 122650008 21458:3379652;1628665:3233940;1628665:3233938;... 1 20141231
29946 641734831 22105131076 50014277 50014815 21458:21906;13227811:51479;13230966:75369014;3... 2 20141016
29947 731030177 41666438142 121394024 50008168 21458:3443560;1628665:3233942;1628665:3233938;... 1 20141016
29948 68515755 13953276547 50012788 28 21458:12376977;2112993:32075;1628665:92012;162... 1 20130729
29949 180436843 23375100402 50012451 50008168 21458:33514;1633959:13343071;33030:29800;33162... 1 20130729
29950 801784345 17629938386 50023670 28 21458:3550980;29154281:231350353;11684888:1045... 1 20130729
29951 124458824 19739113764 50013636 50008168 21458:30992;13658074:9306734;1628665:3233941;1... 1 20140322
29952 602141957 37251457564 50012360 122650008 21458:21599;1628665:29798;1628665:82340;162866... 1 20140322
29953 595095853 41160643364 121364022 50008168 21458:80090256;1628665:29784;1628665:29796;162... 1 20150111
29954 1905258237 42298652641 121452056 50008168 21458:30992;1628665:3233942;1628665:31614;1628... 1 20150111
29955 1957645413 36768778465 121448033 38 6940834:29865;1628149:137593;21475:114226;2275... 1 20140815
29956 1854778218 37200665444 50012361 122650008 21458:3645338;13023209:544768204;122217803:309... 1 20140815
29957 268356658 36932456353 50010236 50014815 21458:10513072;12474507:706291650;3091143:9208... 1 20141027
29958 196272909 10066997901 50009540 50014815 21458:21906;13229910:32056435;2191928:73664723... 1 20141104
29959 23473499 38019470815 50010236 50014815 1628665:61550;1628665:3233940;1628665:3233936;... 1 20141104
29960 816394377 19835118833 50003700 28 24448:73774385;6725953:48332;22044:30715;80047... 1 20130912
29961 164859586 15842319049 50012479 28 NaN 1 20130912
29962 119149466 26396292642 50008875 28 21458:30992;11684888:104528258;21475:11488282;... 1 20130912
29963 704655047 10506866020 50007011 50008168 1628665:3233941;1628665:3233942;1628665:323393... 1 20121206
29964 45662429 20745380642 50010555 50008168 25935:31381;1628665:3233941;1628665:3233942;16... 1 20121206
29965 35711492 16563353438 50010544 50008168 21458:11580;25935:21991;1628665:92012;1628665:... 1 20121206
29966 57747284 35169635909 50010549 50008168 21458:125202070;22019:3228688;22019:3248884;22... 1 20140109
29967 287541325 19778523000 50007011 50008168 21458:112788583;1633959:3523439;3130834:209537... 2 20140109
29968 82915321 12766532512 50011993 28 21475:137325;1628665:3233937;1628665:29798;162... 1 20131008
29969 78259523 18309305134 50013711 50008168 21458:30992;1628665:29778;1628665:29793;163395... 1 20131008
29970 758305789 20177445814 50018860 28 21458:3602856;1628665:29784;1628665:3233941;73... 1 20131008
29971 rows × 7 columns
df.info()
RangeIndex: 29971 entries, 0 to 29970
Data columns (total 7 columns):
user_id 29971 non-null object
auction_id 29971 non-null int64
cat_id 29971 non-null int64
cat1 29971 non-null int64
property 29827 non-null object
buy_mount 29971 non-null int64
day 29971 non-null int64
dtypes: int64(5), object(2)
memory usage: 1.6+ MB
df.head(10)
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
df.columns
Index(['user_id', 'auction_id', 'cat_id', 'cat1', 'property', 'buy_mount',
'day'],
dtype='object')
df['user_id'].head()
0 786295544
1 532110457
2 249013725
3 917056007
4 444069173
Name: user_id, dtype: object
df[['user_id','cat1']].head()
user_id cat1
0 786295544 50022520
1 532110457 28
2 249013725 50014815
3 917056007 50014815
4 444069173 50008168
df[['user_id','cat1']][1:5]
user_id cat1
1 532110457 28
2 249013725 50014815
3 917056007 50014815
4 444069173 50008168
df.loc[3:4]
user_id auction_id cat_id cat1 property buy_mount day
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
df.loc[:,['user_id','buy_mount']].head(10)
user_id buy_mount
0 786295544 2
1 532110457 1
2 249013725 1
3 917056007 2
4 444069173 1
5 152298847 1
6 513441334 1
7 297411659 1
8 82830661 1
9 475046636 1
df.loc[1:3,['user_id','buy_mount']]
user_id buy_mount
1 532110457 1
2 249013725 1
3 917056007 2
df.loc[df.user_id == '532110457']
user_id auction_id cat_id cat1 property buy_mount day
1 532110457 17916191097 50011993 28 21458:11399317;1628862:3251296;21475:137325;16... 1 20131011
df.loc[df.user_id == '532110457',['user_id','buy_mount']]
user_id buy_mount
1 532110457 1
df.loc[(df.user_id == '532110457') | (df.buy_mount > 300),['user_id','buy_mount']]
user_id buy_mount
1 532110457 1
1164 1945590674 1500
5536 2288344467 10000
6241 300205516 600
6627 117730165 2800
9190 462029374 500
10402 32141414 1000
12403 300857121 600
12543 119491758 400
14186 1681976532 340
16213 119395773 700
17821 125495768 410
20490 51409972 306
21482 871858982 350
21989 1671630112 498
25675 173701616 2748
27735 105969610 450
28498 50880819 399
df.iloc[1:3]
user_id auction_id cat_id cat1 property buy_mount day
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
df.iloc[:,1:4]
auction_id cat_id cat1
0 41098319944 50014866 50022520
1 17916191097 50011993 28
2 21896936223 50012461 50014815
3 12515996043 50018831 50014815
4 20487688075 50013636 50008168
5 41840167463 121394024 50008168
6 19909384116 50010557 50008168
7 13540124907 50010542 50008168
8 19948600790 50013874 28
9 10368360710 203527 28
10 15307958346 50018202 38
11 21162876126 50012365 122650008
12 15898050723 50013636 50008168
13 15771663914 50015841 28
14 22532727492 50013711 50008168
15 16078389250 50012359 122650008
16 35086271572 50010544 50008168
17 17436967558 50009540 50014815
18 36153356431 50013207 50008168
19 22058239899 50024147 28
20 19171641742 50018860 28
21 38564176352 50013207 50008168
22 8232924597 211122 38
23 17712372914 121434042 50014815
24 35537441586 50006520 50014815
25 15502618744 50010555 50008168
26 26481508332 121412034 50014815
27 40945285800 50012365 122650008
28 35838498718 50012442 50008168
29 27177784760 121394024 50008168
30 40133707057 50010555 50008168
31 42001753405 121394024 50008168
32 41181827319 50012365 122650008
33 39059292616 121452056 50008168
34 15462429573 50007011 50008168
35 14149079479 50023669 28
36 14653740604 50016704 50022520
37 17610665576 50011993 28
38 14228645401 50018824 38
39 39674261411 121466023 50008168
40 40484992676 50011993 28
41 40070019945 50006602 50008168
42 18678458676 50010566 50008168
43 38451267766 121540027 28
44 40519533209 50010544 50008168
45 19063296909 50006235 50008168
46 19051046285 121398041 28
47 14234909614 121470030 50014815
48 20252281923 50013636 50008168
49 18426669796 50018438 50014815
... ... ... ...
29921 16915013171 50008845 28
29922 10310068684 50012449 50008168
29923 15265710813 50006602 50008168
29924 18836766607 50012788 28
29925 39635136808 50018436 50014815
29926 19704833222 50011993 28
29927 14288242617 50012438 50014815
29928 20959068389 50010544 50008168
29929 37455013590 50023663 28
29930 18220260582 122678023 28
29931 40608095216 50010558 50008168
29932 16684531006 50006095 28
29933 35024165075 50010558 50008168
29934 40599422228 50012428 28
29935 20045810860 50018831 50014815
29936 39339180944 50023591 50022520
29937 40289849863 50013207 50008168
29938 35760225761 50023613 50022520
29939 40798454555 203319 28
29940 35165371283 121394024 50008168
29941 16521677358 50012478 50014815
29942 21129724585 50013207 50008168
29943 18176728510 50013177 28
29944 37844041565 50010555 50008168
29945 42504930457 50012359 122650008
29946 22105131076 50014277 50014815
29947 41666438142 121394024 50008168
29948 13953276547 50012788 28
29949 23375100402 50012451 50008168
29950 17629938386 50023670 28
29951 19739113764 50013636 50008168
29952 37251457564 50012360 122650008
29953 41160643364 121364022 50008168
29954 42298652641 121452056 50008168
29955 36768778465 121448033 38
29956 37200665444 50012361 122650008
29957 36932456353 50010236 50014815
29958 10066997901 50009540 50014815
29959 38019470815 50010236 50014815
29960 19835118833 50003700 28
29961 15842319049 50012479 28
29962 26396292642 50008875 28
29963 10506866020 50007011 50008168
29964 20745380642 50010555 50008168
29965 16563353438 50010544 50008168
29966 35169635909 50010549 50008168
29967 19778523000 50007011 50008168
29968 12766532512 50011993 28
29969 18309305134 50013711 50008168
29970 20177445814 50018860 28
29971 rows × 3 columns
df.iloc[[1,10],[0,2]]
user_id cat_id
1 532110457 50011993
10 734147966 50018202
#iloc 和 loc
df.loc[2:7]#标签
user_id auction_id cat_id cat1 property buy_mount day
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
df.iloc[2:7]#按照顺序(行)
user_id auction_id cat_id cat1 property buy_mount day
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
1.在数据中,直接添加列
2.使用df.insert方法在数据中添加一列
3.drop(labels, axis, inplace = True)的用法
4.labels表示删除的数据,axis表示作用轴,inplace = True表示对原数据生效
5.axis = 0按行操作;axis = 1按列操作
6.使用del函数直接删除其中一列
df.head()
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
df['购买量'] = np.where(df['buy_mount']>3,'高','低')
df.head()
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 低
auction_id = df['auction_id']
del df['auction_id']
df.insert(0,'auction_id_new',auction_id)
df.head()
auction_id_new user_id cat_id cat1 property buy_mount day 购买量
0 41098319944 786295544 50014866 50022520 21458:86755362;13023209:3593274;10984217:21985... 2 20140919 低
1 17916191097 532110457 50011993 28 21458:11399317;1628862:3251296;21475:137325;16... 1 20131011 低
2 21896936223 249013725 50012461 50014815 21458:30992;1628665:92012;1628665:3233938;1628... 1 20131011 低
3 12515996043 917056007 50018831 50014815 21458:15841995;21956:3494076;27000458:59723383... 2 20141023 低
4 20487688075 444069173 50013636 50008168 21458:30992;13658074:3323064;1628665:3233941;1... 1 20141103 低
pd.set_option('display.max_rows',100)#最多显示100行
df.drop(labels=['auction_id_new','购买量'],axis = 1)
user_id cat_id cat1 property buy_mount day
0 786295544 50014866 50022520 21458:86755362;13023209:3593274;10984217:21985... 2 20140919
1 532110457 50011993 28 21458:11399317;1628862:3251296;21475:137325;16... 1 20131011
2 249013725 50012461 50014815 21458:30992;1628665:92012;1628665:3233938;1628... 1 20131011
3 917056007 50018831 50014815 21458:15841995;21956:3494076;27000458:59723383... 2 20141023
4 444069173 50013636 50008168 21458:30992;13658074:3323064;1628665:3233941;1... 1 20141103
5 152298847 121394024 50008168 21458:3408353;13023209:727117752;22009:2741771... 1 20141103
6 513441334 50010557 50008168 25935:21991;1628665:29784;22019:34731;22019:20... 1 20121212
7 297411659 50010542 50008168 21458:60020529;25935:31381;1633959:27247291;16... 1 20121212
8 82830661 50013874 28 21458:11580;21475:137325 1 20121101
9 475046636 203527 28 22724:40168;22729:40278;21458:21817;2770200:24... 1 20121101
10 734147966 50018202 38 21458:3270827;7361532:28710594;7397093:7536994... 2 20121101
11 68547330 50012365 122650008 1628665:3233941;1628665:3233942;1628665:323393... 1 20121123
12 697081418 50013636 50008168 21458:19726868;1633959:179425852;13836282:1290... 1 20121123
13 377550424 50015841 28 1628665:3233941;1628665:3233942;3914866:11580;... 1 20121123
14 88313935 50013711 50008168 1628665:3233941;1628665:3233942;22019:3340598;... 1 20131005
15 25918750 50012359 122650008 21458:3405407;1633959:6186201;1628366:32799;81... 1 20131005
16 350288528 50010544 50008168 21458:61813;25935:21991;1628665:3233938;162866... 1 20131129
17 348090113 50009540 50014815 21458:21910;3110425:30696849;2191928:75373546;... 1 20131129
18 1635282280 50013207 50008168 1628665:29784;1628665:29799;2904342:31004;2201... 1 20131129
19 530850018 50024147 28 21458:205007542;43307470:5543413;2339128:62147... 1 20140210
20 749507708 50018860 28 21458:3602856;1628665:3233941;1628665:3233942;... 1 20140210
21 201088567 50013207 50008168 1628665:3233941;1628665:3233942;1628665:323393... 1 20140502
22 469517728 211122 38 21458:21782;36786:42781029;13023102:6999219;22... 6 20140502
23 691367866 121434042 50014815 21458:49341152;8021059:5525523;6851452:1398669... 1 20140804
24 77193822 50006520 50014815 22277:6262384;21458:30992;1628665:3233941;1628... 2 20140804
25 605678021 50010555 50008168 25935:31381;1628665:3233941;1628665:3233942;16... 1 20130226
26 47702620 121412034 50014815 21458:49341152;11057903:4036007;130475532:7537... 1 20140918
27 763560371 50012365 122650008 21458:30992;1628665:3233939;22007:30338;22007:... 1 20150201
28 408028533 50012442 50008168 21458:3596449;6811831:3446999;13023209:3446999... 1 20141009
29 53566371 121394024 50008168 21458:42090508;1628665:3233941;1628665:3233942... 1 20141009
30 69873877 50010555 50008168 21458:30992;25935:31381;1628665:3233941;162866... 1 20141017
31 1609185254 121394024 50008168 21458:30992;1628665:3233942;1628665:3233936;16... 1 20141228
32 1746148145 50012365 122650008 21458:621749996;13023209:12868;122217803:30916... 1 20141228
33 256475742 121452056 50008168 1628665:29784;1628665:29782;122217801:50793479... 1 20140711
34 405194127 50007011 50008168 21458:35624651;1633959:7320293;1628665:3233941... 1 20120819
35 938309370 50023669 28 21458:4204704;11820090:105550653;11644036:2861... 1 20120819
36 84258337 50016704 50022520 21458:3394654;5261331:4377028;1633959:4377028;... 1 20120819
37 14466144 50011993 28 21458:104000;21475:137325 1 20130327
38 177724549 50018824 38 21475:108284;6933666:96059;33595:16453265;2145... 1 20130327
39 727823869 121466023 50008168 21458:14332755;1628665:3233941;1628665:3233942... 2 20140813
40 659020106 50011993 28 21458:16162126;13023209:10551667;122218042:605... 1 20140813
41 46277938 50006602 50008168 21458:29563;10984217:21985;13023209:3488197;21... 1 20140813
42 827091396 50010566 50008168 21458:46906;13023209:158751187;25935:21991;320... 1 20140911
43 18100946 121540027 28 21458:215485914;125501489:19689726;11945782:78... 1 20140911
44 725813399 50010544 50008168 21458:32270;13023209:669513679;25935:21991;162... 1 20140911
45 1054852159 50006235 50008168 1628665:3233941;21475:17106236;21475:17106365;... 2 20140703
46 262519726 121398041 28 11666049:40203;21458:3961150;17472269:13302841... 1 20140703
47 87207277 121470030 50014815 21458:30992;1628665:3233941;1628665:3233942;16... 1 20140703
48 1053602675 50013636 50008168 21458:216724052;1628665:29798;1628665:29796;25... 1 20140220
49 103125167 50018438 50014815 21458:46896;1628665:3233941;1628665:3233942;21... 16 20140220
... ... ... ... ... ... ...
29921 1372572 50008845 28 21458:30992;1628665:3233941;1628665:3233942;16... 1 20130327
29922 646265934 50012449 50008168 21458:70899896;8539985:3374457;1633959:3374457... 1 20130823
29923 184923046 50006602 50008168 30497:46157;30498:46160;1628665:3233941;162866... 1 20130823
29924 167288532 50012788 28 2112993:29274;122277833:104377;1628665:3233941... 1 20140913
29925 54855720 50018436 50014815 1628665:3233941;1628665:3233942;1628665:323393... 2 20140913
29926 2183330808 50011993 28 21458:119679150;13023209:4067094;122218042:216... 1 20140913
29927 372911996 50012438 50014815 21458:8535341;1628665:3233941;5970350:3402020;... 1 20121222
29928 22414724 50010544 50008168 21458:61834;25935:21990;1628665:3233941;162866... 1 20121222
29929 113446171 50023663 28 11760865:6120557;21458:3314841;122277597:2054;... 1 20140831
29930 522850986 122678023 28 21458:91312408;138253785:653822445;135925585:1... 1 20140831
29931 671912471 50010558 50008168 25935:21991;1628665:3233942;1628665:3233938;12... 1 20140831
29932 2212582093 50006095 28 21458:3532516;8088680:4482698;1628665:3233941;... 1 20150205
29933 712130383 50010558 50008168 21458:30992;25935:31381;1628665:29799;12221780... 1 20150205
29934 326432534 50012428 28 1628665:3233941;1628665:29790;1628665:3233936;... 1 20150110
29935 1663431626 50018831 50014815 21458:3630550;7255245:3630549;27174406:1782825... 2 20150110
29936 1041477880 50023591 50022520 1628665:29790;1628665:29778;1628665:3233938;16... 1 20140710
29937 2078287226 50013207 50008168 21458:100854706;13023209:647423831;22121:11712... 1 20141029
29938 22270319 50023613 50022520 NaN 1 20141029
29939 2074613684 203319 28 122217795:29277;134943792:29774;134943792:2977... 1 20141108
29940 1048135914 121394024 50008168 1628665:3233941;1628665:3233942;1628665:323393... 1 20141108
29941 413188001 50012478 50014815 21458:28155;5434803:3636603;2815901:22583732;1... 1 20130107
29942 474062095 50013207 50008168 21458:21599;1628665:29798;1628665:3233938;1628... 1 20130107
29943 797710454 50013177 28 1628665:3233941;1628665:3233942;1628665:323393... 1 20130107
29944 1716505453 50010555 50008168 21458:30992;25935:31381;1628665:3233941;162866... 1 20141231
29945 1966692323 50012359 122650008 21458:3379652;1628665:3233940;1628665:3233938;... 1 20141231
29946 641734831 50014277 50014815 21458:21906;13227811:51479;13230966:75369014;3... 2 20141016
29947 731030177 121394024 50008168 21458:3443560;1628665:3233942;1628665:3233938;... 1 20141016
29948 68515755 50012788 28 21458:12376977;2112993:32075;1628665:92012;162... 1 20130729
29949 180436843 50012451 50008168 21458:33514;1633959:13343071;33030:29800;33162... 1 20130729
29950 801784345 50023670 28 21458:3550980;29154281:231350353;11684888:1045... 1 20130729
29951 124458824 50013636 50008168 21458:30992;13658074:9306734;1628665:3233941;1... 1 20140322
29952 602141957 50012360 122650008 21458:21599;1628665:29798;1628665:82340;162866... 1 20140322
29953 595095853 121364022 50008168 21458:80090256;1628665:29784;1628665:29796;162... 1 20150111
29954 1905258237 121452056 50008168 21458:30992;1628665:3233942;1628665:31614;1628... 1 20150111
29955 1957645413 121448033 38 6940834:29865;1628149:137593;21475:114226;2275... 1 20140815
29956 1854778218 50012361 122650008 21458:3645338;13023209:544768204;122217803:309... 1 20140815
29957 268356658 50010236 50014815 21458:10513072;12474507:706291650;3091143:9208... 1 20141027
29958 196272909 50009540 50014815 21458:21906;13229910:32056435;2191928:73664723... 1 20141104
29959 23473499 50010236 50014815 1628665:61550;1628665:3233940;1628665:3233936;... 1 20141104
29960 816394377 50003700 28 24448:73774385;6725953:48332;22044:30715;80047... 1 20130912
29961 164859586 50012479 28 NaN 1 20130912
29962 119149466 50008875 28 21458:30992;11684888:104528258;21475:11488282;... 1 20130912
29963 704655047 50007011 50008168 1628665:3233941;1628665:3233942;1628665:323393... 1 20121206
29964 45662429 50010555 50008168 25935:31381;1628665:3233941;1628665:3233942;16... 1 20121206
29965 35711492 50010544 50008168 21458:11580;25935:21991;1628665:92012;1628665:... 1 20121206
29966 57747284 50010549 50008168 21458:125202070;22019:3228688;22019:3248884;22... 1 20140109
29967 287541325 50007011 50008168 21458:112788583;1633959:3523439;3130834:209537... 2 20140109
29968 82915321 50011993 28 21475:137325;1628665:3233937;1628665:29798;162... 1 20131008
29969 78259523 50013711 50008168 21458:30992;1628665:29778;1628665:29793;163395... 1 20131008
29970 758305789 50018860 28 21458:3602856;1628665:29784;1628665:3233941;73... 1 20131008
29971 rows × 6 columns
df.head()#发现刚才删除的两列数据还是在
auction_id_new user_id cat_id cat1 property buy_mount day 购买量
0 41098319944 786295544 50014866 50022520 21458:86755362;13023209:3593274;10984217:21985... 2 20140919 低
1 17916191097 532110457 50011993 28 21458:11399317;1628862:3251296;21475:137325;16... 1 20131011 低
2 21896936223 249013725 50012461 50014815 21458:30992;1628665:92012;1628665:3233938;1628... 1 20131011 低
3 12515996043 917056007 50018831 50014815 21458:15841995;21956:3494076;27000458:59723383... 2 20141023 低
4 20487688075 444069173 50013636 50008168 21458:30992;13658074:3323064;1628665:3233941;1... 1 20141103 低
df.drop(labels=['auction_id_new','购买量'],axis = 1, inplace = True)#替换原数据,使数据生效
df.head()
user_id cat_id cat1 property buy_mount day
0 786295544 50014866 50022520 21458:86755362;13023209:3593274;10984217:21985... 2 20140919
1 532110457 50011993 28 21458:11399317;1628862:3251296;21475:137325;16... 1 20131011
2 249013725 50012461 50014815 21458:30992;1628665:92012;1628665:3233938;1628... 1 20131011
3 917056007 50018831 50014815 21458:15841995;21956:3494076;27000458:59723383... 2 20141023
4 444069173 50013636 50008168 21458:30992;13658074:3323064;1628665:3233941;1... 1 20141103
df.drop(labels=[3,4], axis = 0, inplace = True)#删除行标签3,4;axis默认为0
df.head()
user_id cat_id cat1 property buy_mount day
0 786295544 50014866 50022520 21458:86755362;13023209:3593274;10984217:21985... 2 20140919
1 532110457 50011993 28 21458:11399317;1628862:3251296;21475:137325;16... 1 20131011
2 249013725 50012461 50014815 21458:30992;1628665:92012;1628665:3233938;1628... 1 20131011
5 152298847 121394024 50008168 21458:3408353;13023209:727117752;22009:2741771... 1 20141103
6 513441334 50010557 50008168 25935:21991;1628665:29784;22019:34731;22019:20... 1 20121212
df.drop(labels=range(6,11),axis=0,inplace = True)
df.head()
user_id cat_id cat1 property buy_mount day
0 786295544 50014866 50022520 21458:86755362;13023209:3593274;10984217:21985... 2 20140919
1 532110457 50011993 28 21458:11399317;1628862:3251296;21475:137325;16... 1 20131011
2 249013725 50012461 50014815 21458:30992;1628665:92012;1628665:3233938;1628... 1 20131011
5 152298847 121394024 50008168 21458:3408353;13023209:727117752;22009:2741771... 1 20141103
11 68547330 50012365 122650008 1628665:3233941;1628665:3233942;1628665:323393... 1 20121123
1.在数据中,可以使用rename修改列名称或者行索引名称
2.使用loc方法修改数据
3.使用loc方法查找符合条件的数据
4.条件和条件之间用&或者|连接,分别代表‘且’ 和‘或’
5.使用between和isin选择满足条件的行
import pandas as pd
import os
os.chdir('D:\code\jupyter\course\代码和数据')
df1 = pd.read_csv('sam_tianchi_mum_baby.csv',encoding = 'utf-8', dtype = str)
df1.head()
user_id birthday gender
0 2757 20130311 1
1 415971 20121111 0
2 1372572 20120130 1
3 10339332 20110910 0
4 10642245 20130213 0
df1.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 953 entries, 0 to 952
Data columns (total 3 columns):
user_id 953 non-null object
birthday 953 non-null object
gender 953 non-null object
dtypes: object(3)
memory usage: 22.4+ KB
df1.loc[df1['gender']=='0','gender'] = '女性'
df1.loc[df1['gender'] == '1', 'gender'] = '男性'
df1.loc[df1['gender'] == '2', 'gender'] = '未知'
df1.head(10)
user_id birthday gender
0 2757 20130311 男性
1 415971 20121111 女性
2 1372572 20120130 男性
3 10339332 20110910 女性
4 10642245 20130213 女性
5 10923201 20110830 男性
6 11768880 20120107 男性
7 12519465 20130705 男性
8 12950574 20090708 女性
9 13735440 20120323 女性
df1.rename(columns={
'user_id':'用户ID','birthday':'出生日期','gender':'性别'},inplace = True)
df1.head()
用户ID 出生日期 性别
0 2757 20130311 男性
1 415971 20121111 女性
2 1372572 20120130 男性
3 10339332 20110910 女性
4 10642245 20130213 女性
df1.rename(index = {
3:3555,4:6789}, inplace = True)
df1.head()
用户ID 出生日期 性别
0 2757 20130311 男性
1 415971 20121111 女性
2 1372572 20120130 男性
3555 10339332 20110910 女性
6789 10642245 20130213 女性
df1.iloc[:5]
用户ID 出生日期 性别
0 2757 20130311 男性
1 415971 20121111 女性
2 1372572 20120130 男性
3555 10339332 20110910 女性
6789 <