如何基于pandas python中的特定列合并两个数据帧?

Sai*_*esh 19 python python-2.7 pandas

我必须合并两个数据帧

DF1

company,standard
tata,A1
cts,A2
dell,A3
Run Code Online (Sandbox Code Playgroud)

DF2

company,return
tata,71
dell,78
cts,27
hcl,23
Run Code Online (Sandbox Code Playgroud)

我必须将两个数据帧作为一个数据帧.我需要输出像

company,standard,return
tata,A1,71
cts,A2,27
dell,A3,78
Run Code Online (Sandbox Code Playgroud)

jez*_*ael 40

用途merge:

print (pd.merge(df1, df2, on='company'))
Run Code Online (Sandbox Code Playgroud)

样品:

print (df1)
  company standard
0    tata       A1
1     cts       A2
2    dell       A3

print (df2)
  company  return
0    tata      71
1    dell      78
2     cts      27
3     hcl      23

print (pd.merge(df1, df2, on='company'))
  company standard  return
0    tata       A1      71
1     cts       A2      27
2    dell       A3      78
Run Code Online (Sandbox Code Playgroud)


小智 5

我想我们也可以使用

df1.merge(df2,on='Company')
Run Code Online (Sandbox Code Playgroud)