python数据表的合并_merge
2020-12-13 02:17
标签:github viewer 左右 数据 pytho div panda mic 合并 python数据表的合并_merge 标签:github viewer 左右 数据 pytho div panda mic 合并 原文地址:https://www.cnblogs.com/yellow-hgy/p/11031060.htmlRef:https://nbviewer.jupyter.org/github/pydata/pydata-book/blob/2nd-edition/ch08.ipynb
import pandas as pd
df1 = pd.DataFrame({‘key‘: [‘b‘, ‘b‘, ‘a‘, ‘c‘, ‘a‘, ‘a‘, ‘b‘],
‘data1‘: range(7)})
df2 = pd.DataFrame({‘key‘: [‘a‘, ‘b‘, ‘d‘],
‘data2‘: range(3)})
df1
df2
pd.merge(df1, df2) #不指定on则以两个DataFrame的列名交集做为连接键 ,这里指的是"key"
pd.merge(df1,df2,on = "key")
pd.merge(df1,df2,on = "key",how = "left") #左连接
pd.merge(df1,df2,on = "key",how = "right") #右连接
df2.columns=["key2","data2"] #df2 改名
df2
pd.merge(df1,df2,left_on="key",right_on="key2") ##当左右两个DataFrame的列名不同,指定不同的列明进行merge
#同样可以进行左外连接,右外连接,内链接
pd.merge(df1,df2,left_on="key",right_on="key2",how = "left")
pd.merge(df1,df2,left_on="key",right_on="key2",how = "right")
pd.merge(df1,df2,left_on="key",right_on="key2",how = "inner")
上一篇:ASP.NET MVC 4 (十三) 基于表单的身份验证
下一篇:技术大牛面试 http://www.itmian4.com/forum.php?mod=viewthread&tid=3824