我想通过比较df1和df2中的“否”仅从df2(所有列)中获取数据。
下面是我的3行代码,为此,我无法从df1和df2获取所有列,因此无法从df1修剪字段。如何实现?
我有2个如下所示的pandas数据框:
df1:
no,name,salary
1,abc,100
2,def,105
3,abc,110
4,def,115
5,abc,120
df2:
no,name,salary,dept,addr
1,abc,100,IT1,ADDR1
2,abc,101,IT2,ADDR2
3,abc,102,IT3,ADDR3
4,abc,103,IT4,ADDR4
5,abc,104,IT5,ADDR5
6,abc,105,IT6,ADDR6
7,abc,106,IT7,ADDR7
8,abc,107,IT8,ADDR8
df1 = pd.read_csv("D:\\data\\data1.csv")
df2 = pd.read_csv("D:\\data\\data2.csv")
resDF = pd.merge(df1, df2, on='no' , how='inner')
答案 0 :(得分:1)
我认为您只需要过滤no
列,则不需要on
和how
参数:
resDF = pd.merge(df1[['no']], df2)
或将boolean indexing
与isin
一起过滤:
resDF = df2[df2['no'].isin(df1['no'])]