update 和 left join 合并_Python数据预处理——连接、合并、重塑样本
merge、pd.concat 、reshape
1.橫向連接
- 內(nèi)、外連接 merge
df1.merge(df2,how='inner',on='id') 其中how='left'/'right'/'outer',默認(rèn)是'inner'。
若公共字段在兩表名稱不一致,可以通過left_on和right_on指定:df1.merge(df2,how='inner',left_on='id',right_on='ID')
pd.merge(a,b,left_on=['key1', 'key2'], right_index=True, how='outer')
pd.merge(a,b,how='outer', left_index=True,right_index=True)
- 行索引連接 pd.concat 或數(shù)據(jù)框的join方法
pd.concat([df1,df2],axis=1,join='inner') 默認(rèn)情況下,concat是在axis=0上工作的
df1.join([df2,df3],how='outer')
2.縱向合并 pd.concat
- union all(不去重)
pd.concat([df1,df2],ignore_index=True,axis=0)
axis=0類似于sql中的union all,ignor_index=True表示忽略df1和df2原來的行索引,合并并重新排列索引
- union (去重)
pd.concat([df1,df2],ignore_index=True).drop_duplicates()
drop_duplicates類似于sql中的union
*對axis=0/1 的理解:axis=0代表跨行(down),axis=1代表跨列(across)
3.重塑 reshape stack unstack
stack堆疊、堆積(花括號)、 unstack 不要堆疊 (表結(jié)構(gòu))
總結(jié)
以上是生活随笔為你收集整理的update 和 left join 合并_Python数据预处理——连接、合并、重塑样本的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 创建springboot_【小练习】创建
- 下一篇: python接入微信公众号_Python