[英]Using column values of a data frame to index rows of a multiindex data frame
How can I index the rows of a multiindex data frame如何索引多索引数据框的行
import pandas as pd
import numpy as np
np.random.seed(0)
tuples = list(zip(*[['bar', 'bar', 'baz', 'baz'],['one', 'two', 'one', 'two']]))
idx = pd.MultiIndex.from_tuples(tuples, names=['first', 'second'])
df = pd.DataFrame(np.random.randn(4, 2), index=idx, columns=['A', 'B'])
print(df)
A B
first second
bar one 1.764052 0.400157
two 0.978738 2.240893
baz one 1.867558 -0.977278
two 0.950088 -0.151357
using the columns of a second data frame使用第二个数据框的列
idxDf = pd.DataFrame({'first':['bar','baz'],'second':['one','two']})
print(idxDf)
first second
0 bar one
1 baz two
such that the resulting data frame is这样生成的数据帧是
first second
bar one 1.764052 0.400157
baz two 0.950088 -0.151357
? ?
Obviously, df[idxDf['first','second']]
doesn't work.显然,
df[idxDf['first','second']]
不起作用。
Use DataFrame.merge
with DataFrame.reset_index
and DataFrame.set_index
:使用
DataFrame.merge
与DataFrame.reset_index
和DataFrame.set_index
:
print (df.reset_index().merge(idxDf, on=['first','second']).set_index(['first','second']))
A B
first second
bar one 1.764052 0.400157
baz two 0.950088 -0.151357
Or DataFrame.set_index
:或
DataFrame.set_index
:
print (df.merge(idxDf,
left_index=True,
right_on=['first','second']).set_index(['first','second']))
A B
first second
bar one 1.764052 0.400157
baz two 0.950088 -0.151357
Or DataFrame.set_index
before merge
:或
DataFrame.set_index
之前merge
:
print (df.merge(idxDf.set_index(['first','second']),
left_index=True,
right_index=True))
A B
first second
bar one 1.764052 0.400157
baz two 0.950088 -0.151357
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.