繁体   English   中英

在第二个数据框内的值上连接两个数据框

[英]Join two dataframes on values within the second dataframe

我正在尝试从数据集中的值连接两个数据框:

df1     t0      t1      text0   text1
ID                                  
2133    7.0     3.0     NaN     NaN
1234    10.0    8.0     NaN     NaN
7352    9.0     7.0     NaN     NaN
2500    7.0     6.0     NaN     NaN
3298    10.0    8.0     NaN     NaN

df1(如上所示)

df2     score   text_org
ID                                  
2133    7.0     asdf
2500    7.0     cccc
3298    8.0     ytyt  
2133    3.0     qwer
1234    10.0    pois
7352    9.0     ijsd
7352    7.0     bdcs
3298    10.0    swed
1234    8.0     zzzz
2500    6.0     erer

和df2(如上所示)

我正在尝试合并两个数据帧,以便将df1中的NaN替换为df2中的text_org。 如您所见,我们通过将ID与t0或t1中的得分相匹配来获取文本。 理想情况下,它看起来像这样:

 df1     t0     t1      text0   text1
ID                                  
2133    7.0     3.0     asdf    qwer
1234    10.0    8.0     pois    zzzz
7352    9.0     7.0     ijsd    bdcs
2500    7.0     6.0     cccc    erer
3298    10.0    8.0     swed    ytyt

我试图使用pd.merge-进行联接,但是我却一无所获。 谢谢你的帮助!

你可以先用melt与重塑drop空列text0text1

df = pd.melt(df1.drop(['text0','text1'], axis=1), id_vars='ID', value_name='score')
print (df)
     ID variable  score
0  2133       t0    7.0
1  1234       t0   10.0
2  7352       t0    9.0
3  2500       t0    7.0
4  3298       t0   10.0
5  2133       t1    3.0
6  1234       t1    8.0
7  7352       t1    7.0
8  2500       t1    6.0
9  3298       t1    8.0

然后通过内部联接merge (默认情况下,参数how='inner'是默认值,因此它被忽略了),并且on=['ID','score']也被忽略on=['ID','score']因为在这两个DataFrames中仅这两个列是共同的:

df = pd.merge(df2, df)
print (df)
     ID  score text_org variable
0  2133    7.0     asdf       t0
1  2500    7.0     cccc       t0
2  3298    8.0     ytyt       t1
3  2133    3.0     qwer       t1
4  1234   10.0     pois       t0
5  7352    9.0     ijsd       t0
6  7352    7.0     bdcs       t1
7  3298   10.0     swed       t0
8  1234    8.0     zzzz       t1
9  2500    6.0     erer       t1

上一次通过unstack重塑unstack并通过df1设置列名而没有第一列( [1:] ):

df = df.set_index(['ID','variable']).unstack()
df.columns = df1.columns[1:]
print (df)
        t0   t1 text0 text1
ID                         
1234  10.0  8.0  pois  zzzz
2133   7.0  3.0  asdf  qwer
2500   7.0  6.0  cccc  erer
3298  10.0  8.0  swed  ytyt
7352   9.0  7.0  ijsd  bdcs

通过评论编辑:

你得到:

ValueError:索引包含重复的条目,无法重塑

问题是df2是否按列IDscore重复。

例如,新行添加到末尾,并且具有与第一行相同的IDscore21337.0 )-因此获取重复项:

print (df2)
      ID  score text_org
0   2133    7.0     asdf
1   2500    7.0     cccc
2   3298    8.0     ytyt
3   2133    3.0     qwer
4   1234   10.0     pois
5   7352    9.0     ijsd
6   7352    7.0     bdcs
7   3298   10.0     swed
8   1234    8.0     zzzz
9   2500    6.0     erer
10  2133    7.0  new_val

合并后,您可以检查第一和第二列-为同一IDscore你得到2个值- asdfnew_val ,所以出现错误:

df = pd.merge(df2, df)
print (df)
      ID  score text_org variable
0   2133    7.0     asdf       t0
1   2133    7.0  new_val       t0
2   2500    7.0     cccc       t0
3   3298    8.0     ytyt       t1
4   2133    3.0     qwer       t1
5   1234   10.0     pois       t0
6   7352    9.0     ijsd       t0
7   7352    7.0     bdcs       t1
8   3298   10.0     swed       t0
9   1234    8.0     zzzz       t1
10  2500    6.0     erer       t1

解决方案是使用一些pivot_table函数的pivot_table或删除df2中的重复项(例如,使用drop_duplicates ):

#aggregate function is first
df3 = df.pivot_table(index='ID', columns='variable', aggfunc='first')
df3.columns = df1.columns[1:]
print (df3)
      t0 t1 text0 text1
ID                     
1234  10  8  pois  zzzz
2133   7  3  asdf  qwer
2500   7  6  cccc  erer
3298  10  8  swed  ytyt
7352   9  7  ijsd  bdcs

#aggregate function is last
df4 = df.pivot_table(index='ID', columns='variable', aggfunc='last')
df4.columns = df1.columns[1:]
print (df4)
      t0 t1    text0 text1
ID                        
1234  10  8     pois  zzzz
2133   7  3  new_val  qwer
2500   7  6     cccc  erer
3298  10  8     swed  ytyt
7352   9  7     ijsd  bdcs

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM