简体   繁体   中英

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.

Use DataFrame.merge with DataFrame.reset_index and 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 :

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 :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM