2015-04-08 110 views
1

我想使用MultiIndex值從熊貓數據框中刪除行。熊貓數據框通過多索引丟棄行

我已經嘗試了很多東西,但我在下面我認爲更接近。 (其實我會解釋完整的問題,因爲可能會有一種替代解決方案使用完全不同的方法)。從相關矩陣中,我想得到更多關聯的列對。我用unstack,並把結果放在一個數據幀:

In [263]: corr_df = pd.DataFrame(total.corr().unstack()) 

然後得到較高的相關性(其實我應該得到的底片以及)。

In [264]: high = corr_df[(corr_df[0] > 0.5) & (corr_df[0] < 1.0)] 

In [236]: print high 
                0 
residual sugar  density    0.552517 
free sulfur dioxide total sulfur dioxide 0.720934 
total sulfur dioxide free sulfur dioxide 0.720934 
        wine     0.700357 
density    residual sugar  0.552517 
wine     total sulfur dioxide 0.700357 

足夠關閉,但有重複,這實際上是相關矩陣的要點。爲了清除它們,我的想法是迭代的高值,以消除重複:

In [267]: 
for row in high.iterrows(): 
    print row[0][0], ",", row[0][1] 
    print high.loc[row[0][1]].loc[row[0][0]].index 
    high.drop(high.loc[row[0][1]].loc[row[0][0]].index) 
residual sugar , density 
Int64Index([0], dtype='int64') 
--------------------------------------------------------------------------- 
KeyError         Traceback (most recent call last) 
<ipython-input-267-1258da2a4772> in <module>() 
     2  print row[0][0], ",", row[0][1] 
     3  print high.loc[row[0][1]].loc[row[0][0]].index 
----> 4  high.drop(high.loc[row[0][1]].loc[row[0][0]].index) 

... 
[huge stack of errors] 
... 
KeyError: 0 

方法drop完美地工作時,指數是正常的(見drop),但是,我怎麼建立label當我得到MultiIndex

回答

2
corr_df = pd.DataFrame(
{'residual sugar': [1, 0, 0, 0.552517, 0], 
'free sulfur dioxide': [0, 1, 0.720934, 0, 0], 
'total sulfur dioxide': [0, 0.720934, 1, 0, 0.700357], 
'density': [0.552517, 0, 0, 1, 0], 
'wine': [0, 0, 0.700357, 0, 1]}, 
index=['residual sugar', 'free sulfur dioxide', 'total sulfur dioxide', 'density', 'wine']).unstack() 

# Notice the slight modification to the original 
high = corr_df[(corr_df > 0.5) & (corr_df < 1.0)] 

# Sort by index, then values 
high.sort_index() 
high.sort() 

# Drop every other value (e.g. just take the evens) 
result = high.iloc[[count for count, _ in enumerate(high) if count % 2 == 0]] 
>>> result 
density    residual sugar   0.552517 
total sulfur dioxide wine     0.700357 
free sulfur dioxide total sulfur dioxide 0.720934