如何比较不同数据框的列?

时间:2019-02-11 11:17:20

标签: python-3.x pandas numpy

数据帧1

Team      region
xxxx       ppl
yyyy       olk
zzzz       ppl
xyxy       ioj
yzyz       olk
xzxz       ioj

datframe 2

players     Team      
 chin       xxxx      
 shan       yyyy       
 fav        zzzz       
 janb      xyxy       
  jj      yzyz       
  nbhg     xzxz       
  ggdfg    okk
  jhjuf    kkdu
  hgdf     xyxy

如果数据框1团队==数据框2,则打印区域。

必需的输出应为:

players     Team    region    
 chin       xxxx     ppl     
 shan       yyyy     olk     
 fav        zzzz     ppl  
 janb       xyxy     ioj  
  jj        yzyz     olk  
  nbhg      xzxz     ioj  
  hgdf      xyxy

1 个答案:

答案 0 :(得分:1)

使用:

df2.combine_first(df1)

   Team players region
0  xxxx    chin    ppl
1  yyyy    shan    olk
2  zzzz     fav    ppl
3  xyxy    janb    ioj
4  yzyz      jj    olk
5  xzxz    nbhg    ioj
6   okk   ggdfg    NaN
7  kkdu   jhjuf    NaN
8  xyxy    hgdf    NaN