gpt4 book ai didi

python - 将多索引数据帧与数据帧连接起来

转载 作者:太空宇宙 更新时间:2023-11-04 09:51:26 25 4
gpt4 key购买 nike

我正在尝试连接 2 个数据帧 df1df2df1 是一个多索引数据帧,df2 的行数少于 df1

import pandas as pd
import numpy as np
arrays = [['bar', 'bar', 'baz', 'baz', 'foo', 'foo', 'qux', 'qux'],
['one', 'two', 'one', 'two', 'one', 'two', 'one', 'two']]
tuples = list(zip(*arrays))
index = pd.MultiIndex.from_tuples(tuples, names=['first', 'second'])
df1 = pd.DataFrame(np.random.randn(8), index=index)

df1
Out[15]:
0
first second
bar one -0.185560
two -2.358254
baz one 1.130550
two 1.441708
foo one -1.163076
two 1.776814
qux one -0.811836
two 0.389500

df2 = pd.DataFrame(data=[0,1,0,1],index=['bar','baz','foo', 'qux'],columns=['label'])

df2
Out[18]:
label
bar 0
baz 1
foo 0
qux 1

想要的结果应该是这样的:

df3
Out[18]:
0 label
first second
bar one -0.185560 0
two -2.358254 0
baz one 1.130550 1
two 1.441708 1
foo one -1.163076 0
two 1.776814 0
qux one -0.811836 1
two 0.389500 1

最佳答案

另一种方法是只在第二级reset_index,然后你可以只添加将对齐第一级索引值的列,然后再次设置索引:

In[52]:
df3 = df1.reset_index(level=1)
df3['label'] = df2['label']
df3 = df3.set_index([df3.index, 'second'])
df3

Out[52]:
0 label
first second
bar one 0.957417 0
two -0.466755 0
baz one 1.064326 1
two 1.036983 1
foo one -1.319737 0
two 0.064465 0
qux one -0.237232 1
two -0.511889 1

关于python - 将多索引数据帧与数据帧连接起来,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47650563/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com