2017-03-17 89 views
1

另一列的最大值下降對齊行我有一個名爲auDataFrame從中我要刪除的行,其中date與觀測哪裏bal==bal.max()對齊。例如,如果bal==bal.max()2009-08-01關聯,那麼我想刪除其中的所有其他觀察值date=='2009-08-01'。下面是我做過嘗試,但都嘗試導致ValueError: Series lengths must match to compare其中date與大熊貓

au = au[au.date != au.date[au.bal==au.bal.max()]] 
au = au[au.date != au.date[au.bal==au.bal.max()].values] 

回答

3

使用idxmax和@ jezrael的設置

設置

au = pd.DataFrame({'bal':[1,2,3,4], 
        'date':['2009-08-01','2009-08-01','2009-08-02', '2009-08-02'], 
        'C':[7,8,9,1]}) 

解決方案

dmax = au.date.loc[au.bal.idxmax()] 
au[au.date != dmax] 

    C bal  date 
0 7 1 2009-08-01 
1 8 2 2009-08-01 
2

我想你需要從一個項目Series通過itemvalues與[0]選擇firts價值得到標:

au = pd.DataFrame({'bal':[1,2,3,4], 
        'date':['2009-08-01','2009-08-01','2009-08-02', '2009-08-02'], 
        'C':[7,8,9,1]}) 

print (au) 
    C bal  date 
0 7 1 2009-08-01 
1 8 2 2009-08-01 
2 9 3 2009-08-02 
3 1 4 2009-08-02 


print (au[au.date != au.loc[au.bal==au.bal.max(), 'date'].item()]) 
    C bal  date 
0 7 1 2009-08-01 
1 8 2 2009-08-01 

idxmax解決方案 - 與date首先創建Series

print (au.set_index('date').bal.idxmax()) 
2009-08-02 

au = au[au.date != au.set_index('date').bal.idxmax()] 
print (au) 
    C bal  date 
0 7 1 2009-08-01 
1 8 2 2009-08-01