2017-02-23 81 views
0

我有一個數據框,我嘗試從該值中刪除項目。基於條件語句從.loc中刪除項目

for i in range (1, len(df['column1'])): 
    if df['column1'].iloc[i][0] < 2.5: 
     del df['column1'].iloc[i] 

收到的錯誤:

AttributeError       Traceback (most recent call last) 
<ipython-input-80-8b343357b723> in <module>() 
    16 for i in range (1, len(df_agg2['water_amount']-1)): 
    17  if df_agg2['water_amount'].iloc[i][0] < 2.5: 
---> 18   del df_agg2['water_amount'].iloc[i] 

AttributeError: __delitem__ 

例如:

df['column1'].iloc[1] 

回報:

sum 1.422883 
Name: 4, dtype: float64 

和,

df['column1'].iloc[1][0] 

回報:

1.4228829999999981 

我怎樣才能避免這種情況我上面得到,爲了如果是小於2.5刪除項目的AttributeError的?

+0

是否要刪除'column1' <2.5的整行? – languitar

+0

您可以發佈數據框df的示例數據嗎? –

+0

@languitar是的,那是我的目標。 – Gary

回答

1

如果您想根據列從框架中刪除行,您可以選擇像這樣的反轉:

In [7]: df = pd.DataFrame({'foo': ['test', 'this', 'not'], 'column1': [13, 0.2, 10]}) 

In [8]: df 
Out[8]: 
    column1 foo 
0  13.0 test 
1  0.2 this 
2  10.0 not 

In [9]: df[df.column1 >= 2.5] 
Out[9]: 
    column1 foo 
0  13.0 test 
2  10.0 not 
1

你的問題並不清楚,假設你需要刪除單元格時,它是小於2.5。只要記住,你不能真正去除細胞,而不是更換電池與np.Nan

df.ix[df.column1 <2.5, 'column1'] = np.NaN 

,也可以刪除整個行如果該值小於2.5

print df[df.joint1_pt >2.5]