2017-02-23 45 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]