[英]How to Drop rows in DataFrame by conditions on column values
I created code to drop some rows according to certain condition:我创建了代码以根据特定条件删除一些行:
df3 = df_clean[(df_clean['group'] == 'treatment') & (df_clean['landing_page'] != 'new_page')].index
df2 = df_clean.drop(df3 , inplace=True)
df2.head()
but I got this error: AttributeError: 'NoneType' object has no attribute 'head'但我得到了这个错误: AttributeError: 'NoneType' object has no attribute 'head'
You can do something like this.你可以做这样的事情。 I dont have your dataset so using my own.
我没有你的数据集,所以使用我自己的。
import pandas as pd
df = pd.DataFrame({'A':[100,200,300,400,500,600,700,800],
'B':['apple','banana','apple','banana',
'apple','banana','apple','banana']})
print (df)
df1 = df[(df['A'] >400) & (df['B'] == 'apple')]
print (df1)
If I want to get all the rows with 'apple' and values > 400, then, df1 will give me the result.如果我想获取所有具有“apple”且值 > 400 的行,那么 df1 会给我结果。
A B
0 100 apple
1 200 banana
2 300 apple
3 400 banana
4 500 apple
5 600 banana
6 700 apple
7 800 banana
A B
4 500 apple
6 700 apple
Assign df_clean to df2 without the inplace将df_clean分配给df2而不使用 inplace
df2 = df_clean.drop(df3)
Or leave the inplace without assigning, here df_clean will be your output或者离开原地而不分配,这里df_clean将是你的 output
df_clean.drop(df3 , inplace=True)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.