簡體   English   中英

使用數據框的列值來索引多索引數據框的行

[英]Using column values of a data frame to index 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

使用第二個數據框的列

idxDf = pd.DataFrame({'first':['bar','baz'],'second':['one','two']})
print(idxDf)
  first second
0   bar    one
1   baz    two

這樣生成的數據幀是

first second
bar   one     1.764052  0.400157
baz   two     0.950088 -0.151357

?

顯然, df[idxDf['first','second']]不起作用。

使用DataFrame.mergeDataFrame.reset_indexDataFrame.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

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

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM