【发布时间】:2014-07-19 04:48:36
【问题描述】:
我经常有一个具有大型多索引的数据帧,以及一个具有多索引的辅助数据帧,它是较大的一个子集。辅助数据帧通常是某种查找表。我经常想将查找表中的列添加到更大的数据框中。主 DataFrame 通常非常大,所以我想高效地执行此操作。
这是一个虚构的例子,我在其中构造了两个数据框df1 和df2
import pandas as pd
import numpy as np
arrays = [['sun', 'sun', 'sun', 'moon', 'moon', 'moon', 'moon', 'moon'],
['summer', 'winter', 'winter', 'summer', 'summer', 'summer', 'winter', 'winter'],
['one', 'one', 'two', 'one', 'two', 'three', 'one', 'two']]
tuples = list(zip(*arrays))
index = pd.MultiIndex.from_tuples(tuples, names=['Body', 'Season','Item'])
df1 = pd.DataFrame(np.random.randn(8,2), index=index,columns=['A','B'])
index2= pd.MultiIndex.from_tuples([('sun','summer'),('sun','winter'),('moon','summer'),('moon','winter')],
names=['Body','Season'])
df2 = pd.DataFrame(['Good','Bad','Ugly','Confused'],index=index2,columns = ['Mood'])
提供数据框:
df1
A B
Body Season Item
sun summer one -0.409372 0.638502
winter one 1.448772 -1.460596
two -0.495634 -0.839063
moon summer one 1.296035 -1.439349
two -1.002667 0.508394
three -1.247748 -0.645782
winter one -1.848857 -0.858759
two 0.559172 2.202957
df2
Mood
Body Season
sun summer Good
winter Bad
moon summer Ugly
winter Confused
现在,假设我想将 df2 中的列添加到 df1?这条线是我能找到的唯一方法:
df1 = df1.reset_index().join(df2,on=['Body','Season']).set_index(df1.index.names)
导致:
A B Mood
Body Season Item
sun summer one -0.121588 0.272774 Good
winter one 0.233562 -2.005623 Bad
two -1.034642 0.315065 Bad
moon summer one 0.184548 0.820873 Ugly
two 0.838290 0.495047 Ugly
three 0.450813 -2.040089 Ugly
winter one -1.149993 -0.498148 Confused
two 2.406824 -2.031849 Confused
[8 rows x 3 columns]
它有效,但是这种方法有两个问题。首先,这条线很丑。需要重置索引,然后重新创建多索引,使这个简单的操作看起来不必要地复杂。其次,如果我理解正确的话,每次我运行 reset_index() 和 set_index() 时,都会创建一个数据帧的副本。我经常使用非常大的数据帧,这似乎非常低效。
有没有更好的方法来做到这一点?
【问题讨论】:
-
您可以随时将
inplace=True传递给reset_index/set_index