使用MultiIndex分配给Pandas DataFrame的整个列?

68bkxrlz  于 2023-03-21  发布在  其他
关注(0)|答案(1)|浏览(191)

我有一个带有MultiIndex的DataFrame(名为midx_df),我想将另一个带有单级索引的DataFrame(名为sour_df)的整个列的值分配给midx_df
sour_df的所有索引值都存在于midx_df的顶层索引中,我需要指定一级索引来添加/修改具有相同一级索引的行的所有值。
例如:

beg_min = pd.to_datetime('2023/03/18 18:50', yearfirst=True)
end_min = pd.to_datetime('2023/03/18 18:53', yearfirst=True)
minutes = pd.date_range(start=beg_min, end=end_min, freq='1min')
actions = ['Buy', 'Sell']
m_index = pd.MultiIndex.from_product([minutes, actions], names=['time', 'action'])
sour_df = pd.DataFrame(index=minutes, columns=['price'])
sour_df.index.rename('time', inplace=True)
sour_df.loc[minutes[0], 'price'] = 'b0'
sour_df.loc[minutes[1], 'price'] = 'b1'
sour_df.loc[minutes[3], 'price'] = 'b2'

midx_df = pd.DataFrame(index=m_index, columns=['price'])
print(midx_df)

midx_df.loc[(beg_min, 'Buy'), 'price'] = 123    # works but only for one row!
midx_df.loc[(end_min, 'Buy')]['price'] = 124    # doesn't work!
print(midx_df)

midx_df.loc[(slice(None), 'Buy'), 'price'] = sour_df    # doesn't work!
print(midx_df)

midx_df.loc[(slice(None), 'Buy'), 'price'] = sour_df['price']    # doesn't work!
print(midx_df)

#midx_df.loc[(slice(None), 'Buy')]['price'] = sour_df['price']    # doesn't work!
#print(midx_df)

midx_df.loc[pd.IndexSlice[:, 'Buy'], :] = sour_df    # doesn't work!
print(midx_df)

请告诉我正确的方法,谢谢!!!

aemubtdh

aemubtdh1#

这是一个有趣的问题。这里的问题是你的索引没有对齐:('time', 'action') vs 'time',因此pandas无法设置正确的值。
你必须重用midx_df的索引来重新索引sour_dfpd.concat可以用来完成这个任务:

midx_df.loc[(slice(None), 'Buy'), 'price'] = \
    pd.concat([sour_df], keys=['Buy'], names=['action']).swaplevel()
print(midx_df)

# Output
                           price
time                action      
2023-03-18 18:50:00 Buy       b0
                    Sell     NaN
2023-03-18 18:51:00 Buy       b1
                    Sell     NaN
2023-03-18 18:52:00 Buy      NaN
                    Sell     NaN
2023-03-18 18:53:00 Buy       b2
                    Sell     NaN

或者使用pd.MultiIndex.from_product

midx_df.loc[(slice(None), 'Buy'), 'price'] = \
    sour_df.set_index(pd.MultiIndex.from_product([sour_df.index, ['Buy']]))

详情:

>>> midx_df.loc[(slice(None), 'Buy'), 'price']
time                 action
2023-03-18 18:50:00  Buy       NaN
2023-03-18 18:51:00  Buy       NaN
2023-03-18 18:52:00  Buy       NaN
2023-03-18 18:53:00  Buy       NaN
Name: price, dtype: object

>>> pd.concat([sour_df], keys=['Buy'], names=['action']).swaplevel()
                           price
time                action      
2023-03-18 18:50:00 Buy       b0
2023-03-18 18:51:00 Buy       b1
2023-03-18 18:52:00 Buy      NaN
2023-03-18 18:53:00 Buy       b2

>>> sour_df.set_index(pd.MultiIndex.from_product([sour_df.index, ['Buy']]))
                        price
time                         
2023-03-18 18:50:00 Buy    b0
2023-03-18 18:51:00 Buy    b1
2023-03-18 18:52:00 Buy   NaN
2023-03-18 18:53:00 Buy    b2

现在,索引已很好地对齐以设置值。

相关问题