简体   繁体   English

如何通过列值的条件删除 DataFrame 中的行

[英]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 inplacedf_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.

相关问题 按等于无值的列值条件在DataFrame中删除行 - Drop rows in DataFrame by conditions on column values equal to None 如何根据特定列中的空值从数据框中删除行? - How to drop rows from a dataframe as per null values in a specific column? 按列值删除 Pandas DataFrame 中的行(文本) - Drop rows in Pandas DataFrame by Column values (text) 在 Pandas 数据框中在多个条件下(基于 2 列)删除行 - Drop rows on multiple conditions (based on 2 column) in pandas dataframe 如何根据考虑其他 dataframe 的条件删除 pandas dataframe 行 - How to drop pandas dataframe rows based on conditions that consider other dataframe 如何一次读取熊猫数据框的两行两列,并在这些行/列值上应用条件? - How to read two rows and two columns of a pandas dataframe at once and apply conditions on those rows/column values? 如何在熊猫数据框中的列中删除带有“nan”的行? - how to drop rows with 'nan' in a column in a pandas dataframe? 如何根据复杂条件删除特定的 pandas dataframe 行 - How to drop specific pandas dataframe rows based on complex conditions 有没有办法通过将列值与列表中的值进行比较来从 dataframe 中删除行? - Is there a way to drop rows from a dataframe by comparing a column value to values in list? 从 dataframe 中删除值在整列中仅出现一次的行 - Drop rows from dataframe with the values occuring only once in the whole column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM