在 pandas 中合并多索引和单索引数据帧 [英] Merge multi-indexed with single-indexed data frames in pandas

查看:75
本文介绍了在 pandas 中合并多索引和单索引数据帧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个数据框. df1是多索引的:

I have two dataframes. df1 is multi-indexed:

                value
first second    
a     x         0.471780
      y         0.774908
      z         0.563634
b     x         -0.353756
      y         0.368062
      z         -1.721840

和df2:

      value
first   
a     10
b     20

如何将两个数据帧仅与一个多索引合并,在这种情况下为第一个"索引?所需的输出将是:

How can I merge the two data frames with only one of the multi-indexes, in this case the 'first' index? The desired output would be:

                value1      value2
first second    
a     x         0.471780    10
      y         0.774908    10
      z         0.563634    10
b     x         -0.353756   20
      y         0.368062    20
      z         -1.721840   20

推荐答案

您可以使用

You could use get_level_values:

firsts = df1.index.get_level_values('first')
df1['value2'] = df2.loc[firsts].values

注意:您几乎正在做 join (除了df1是MultiIndex)...所以可能有一种更简洁的方式来描述这一点...

Note: you are almost doing a join here (except the df1 is MultiIndex)... so there may be a neater way to describe this...

.

在一个示例中(类似于您所拥有的):

In an example (similar to what you have):

df1 = pd.DataFrame([['a', 'x', 0.123], ['a','x', 0.234],
                    ['a', 'y', 0.451], ['b', 'x', 0.453]],
                   columns=['first', 'second', 'value1']
                   ).set_index(['first', 'second'])
df2 = pd.DataFrame([['a', 10],['b', 20]],
                   columns=['first', 'value']).set_index(['first'])

firsts = df1.index.get_level_values('first')
df1['value2'] = df2.loc[firsts].values

In [5]: df1
Out[5]: 
              value1  value2
first second                
a     x        0.123      10
      x        0.234      10
      y        0.451      10
b     x        0.453      20

这篇关于在 pandas 中合并多索引和单索引数据帧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆