数据处理之数据准备
1、合并merge():pandas数据合并操作,类似与SQL语言中的join,使用一个或多个键将数据合并在一起。merge()函数默认执行的是内连接。
pd.merge(frame1,frame2)
在实际应用中,最好指定按照那一列合并:
pd.merge(frame1,frame2,on='first_name')
pd.merge(frame1,frame2,on='last_name')
如果两个数据表中合并的基准列名称不一致,可以用left_on和right_on来指定第一个表和第二个表的基准列。
pd.merge(frame1,frame2,left_on='first_name',right_on='ming')
和join操作类似,也有左连接,右连接和外连接:
pd.merge(frame1.frame2, on='first_name',how='outer')
pd.merge(frame1,frame2,on='name',how='left')
pd.merge(frame1,frame2,on='last_name',how='right')
如果需要合并多个列:
pd.merge(frame1,frame2,on=['first_name','last_name'],how='outer')
也可以根据索引合并,将索引相同的行直接合并在一起:
pd.merge(frame1,frame2,left_index='true',right_index='true')
import pandas as pd
import numpy as np
frame1=pd.DataFrame(np.arange(16).reshape((4,4)))
frame2=pd.DataFrame(np.arange(1,17).reshape((4,4)))
frame2.columns=['a','b','c','d']
frame3=pd.merge(frame1,frame2,left_on=1,right_on='b')
frame4=pd.merge(frame1,frame2,left_index=True,right_index=True)
print(frame3)
print('--------')
print(frame4)
Empty DataFrame
Columns: [0, 1, 2, 3, a, b, c, d]
Index: []
--------
0 1 2 3 a b c d
0 0 1 2 3 1 2 3 4
1 4 5 6 7 5 6 7 8
2 8 9 10 11 9 10 11 12
3 12 13 14 15 13 14 15 16
在pandas中,用join()函数执行索引合并会更方便,前提是合并的两个数据里列名称没有相同的,而且会将所有的索引列出来,没有数据的元素为NaN。
import pandas as pd
import numpy as np
frame1=pd.DataFrame(np.arange(16).reshape((4,4)))
frame2=pd.DataFrame(np.arange(9).reshape((3,3)))
frame2.columns=['a','b','c']
frame4=pd.merge(frame1,frame2,left_index=True,right_index=True)
frame5=frame1.join(frame2)
print(frame4)
print('-------')
print(frame5)
结果:
0 1 2 3 a b c
0 0 1 2 3 0 1 2
1 4 5 6 7 3 4 5
2 8 9 10 11 6 7 8
-------
0 1 2 3 a b c
0 0 1 2 3 0.0 1.0 2.0
1 4 5 6 7 3.0 4.0 5.0
2 8 9 10 11 6.0 7.0 8.0
3 12 13 14 15 NaN NaN NaN
2、拼接:concat()函数默认使用axis=0这条轴拼接数据,也就是上下拼接,也可以设置axis=1按照左右拼接。
frame1=pd.DataFrame(np.arange(9).reshape((3,3)))
frame2=pd.DataFrame(np.random.rand(9).reshape((3,3)))
frame3=pd.concat([frame1,frame2])
frame4=pd.concat([frame1,frame2],axis=1)
frame5=pd.DataFrame(np.random.rand(9).reshape((3,3)),index=[3,5,6])
frame6=pd.concat([frame1,frame5],axis=1)
print(frame3)
print('-----')
print(frame4)
print('------')
print(frame6)
结果为:
0 1 2
0 0.000000 1.000000 2.000000
1 3.000000 4.000000 5.000000
2 6.000000 7.000000 8.000000
0 0.030952 0.237399 0.236865
1 0.221911 0.364267 0.431113
2 0.694930 0.307513 0.426365
-----
0 1 2 0 1 2
0 0 1 2 0.030952 0.237399 0.236865
1 3 4 5 0.221911 0.364267 0.431113
2 6 7 8 0.694930 0.307513 0.426365
------
0 1 2 0 1 2
0 0.0 1.0 2.0 NaN NaN NaN
1 3.0 4.0 5.0 NaN NaN NaN
2 6.0 7.0 8.0 NaN NaN NaN
3 NaN NaN NaN 0.837020 0.051684 0.655334
5 NaN NaN NaN 0.913895 0.117463 0.832518
6 NaN NaN NaN 0.518409 0.134704 0.063010
3、轴向转换:
入栈(stacking)把列转换为行:
import pandas as pd
import numpy as np
frame1=pd.DataFrame(np.arange(9).reshape((3,3)),index=['blue','green','white'],columns=['ball','pen','pencil'])
frame2=frame1.stack()
print(frame2)
结果为
blue ball 0
pen 1
pencil 2
green ball 3
pen 4
pencil 5
white ball 6
pen 7
pencil 8
dtype: int32
unstack()操作为逆操作,
frame2=frame1.stack()
frame3=frame2.unstack()
print(frame3)
结果为:
ball pen pencil
blue 0 1 2
green 3 4 5
white 6 7 8
unstack()可以传入层级的编号来进行层级操作:
frame2=frame1.stack()
#frame3=frame2.unstack()
frame4=frame2.unstack(0)
print(frame4)
结果为:
blue green white
ball 0 3 6
pen 1 4 7
pencil 2 5 8
4、长格式向宽格式转换的pivot()函数:
例如元数据为,
import pandas as pd
import numpy as np
frame1=pd.DataFrame({'color':['white','white','white','red','red','red','black','black','black'],
'item':['ball','pen','mug','ball','pen','mug','ball','pen','mug'],
'value':np.random.rand(9)})
color item value
0 white ball 0.364847
1 white pen 0.123006
2 white mug 0.743680
3 red ball 0.861445
4 red pen 0.081746
5 red mug 0.118125
6 black ball 0.520116
7 black pen 0.346283
8 black mug 0.820476
选择color作为主键,item作为第二主键
frame1=pd.DataFrame({'color':['white','white','white','red','red','red','black','black','black'],
'item':['ball','pen','mug','ball','pen','mug','ball','pen','mug'],
'value':np.random.rand(9)})
frame2=frame1.pivot('color','item')
value
item ball mug pen
color
black 0.172944 0.435275 0.089109
red 0.478604 0.612887 0.279634
white 0.107988 0.380519 0.770569
还有一个函数pd.pivot_table()
frame3=pd.pivot_table(frame1,value='price',columns='price',index=['site','vender','date'])
frame1=pd.DataFrame({'color':['white','white','white','red','red','red','black','black','black'],
'item':['ball','pen','mug','ball','pen','mug','ball','pen','mug'],
'item1':['A','B','B','C','A','C','C','C','C'],
'value':np.random.rand(9)})
frame3=pd.pivot_table(frame1,values='value',columns='color',index=['item','item1'])
结果为:
color black red white
item item1
ball A NaN NaN 0.575578
C 0.234597 0.651696 NaN
mug B NaN NaN 0.505687
C 0.652943 0.618806 NaN
pen A NaN 0.426786 NaN
B NaN NaN 0.051949
C 0.796792 NaN NaN
4、删除:
删除一列用del命令,指定列名称
del frame1['item']
删除多列,用drop()函数
frame1.drop(['item','item1'])