DataFrame的merge(合并)操作
首先
import numpy as np
import pandas as pd
from pandas import Series,DataFrame
创建df1 df2
df1 = DataFrame({'key':['X','Y','Z','X'],'data_set_1':[1,2,3,4]})
print(df1)
df2 = DataFrame({'key':['X','B','C'],'data_set_2':[4,5,6]})
print(df2)
显示df1 和 df2
合并操作…
print(pd.merge(df1,df2))
print(pd.merge(df1,df2,on = 'key',how = 'right'))
print(pd.merge(df1,df2,on = 'key',how = 'left'))
Concatenate和combine
创建arr1和arr2
import numpy as np
import pandas as pd
from pandas import Series,DataFrame
arr1 = np.arange(9).reshape(3,3)
print(arr1)
arr2 = np.arange(9).reshape(3,3)
print(arr2)
竖着连接,增加行数
print(np.concatenate([arr1,arr2]))#
结果
横着连接,增加列数
print(np.concatenate([arr1,arr2],axis=1))
创建Series并且进行两种方向的连接
s1 = Series([1,2,3],index=['X','Y','Z'])
print(s1)
s2 = Series([4,5],index=['A','B'])
print(s2)
print(pd.concat([s1,s2]))
另一个方向的连接
print(pd.concat([s1,s2],axis=1))
DataFrame的concat
创建
df1 = DataFrame(np.random.randn(4,3),columns=['X','Y','Z'])
print(df1)
df2 = DataFrame(np.random.randn(4,3),columns=['X','Y','A'])
print(df2)
连接
print(pd.concat([df1,df2]))
横着连接
print(pd.concat([df1,df2],axis=1))
第二个操作Combine(填充)
s1 = Series([2,np.nan,4,np.nan],index=['A','B','C','D'])
print(s1)
s2 = Series([1,2,3,4],index=['A','B','C','D'])
print(s2)
用s2去填充s1,如过s1对应位置空则填充,否则保持s1
print(s1.combine_first(s2))#用S2去填充S1
DataFrame
df1 = DataFrame({
'X':[1,np.nan,3,np.nan],
'Y':[5,np.nan,7,np.nan],
'Z':[9,np.nan,11,np.nan]
})
print(df1)
df2 = DataFrame({
'Z':[np.nan,10,np.nan,12],
'A':[1,2,3,4]
})
print(df2)
print(df1.combine_first(df2))
用z和A去填充…