首页 > 解决方案 > Drop rows from Dataframe

问题描述

I am attempting to drop rows from my dataframe that do meet a set of conditions. However it doesn't seem to be working.

Below are the two version which I have tried so far without success:

Attempt 1

df = df.drop(df[(df['Factorization'] != 0.5) & (df['Value'] != 30) & (df['Total'] == None)].index)

Attempt 2

df.drop(df[(df['Factorization'] != 0.5) & (df['Value'] != 20) & (df['Total'] == None)].index, inplace = True)

Please can someone point out where I am going wrong.

标签: pythonpandasdataframedrop

解决方案


One way to go around it is by not using drop by, but instead redefining the df to exclude those conditions by adding ~ ahead of the df

df = df[~((df['Factorization'] != 0.5) & (df['Value'] != 30) & (df['Total'] == None)]))

I may or may not be using one too many () in the code. Please give it a try


推荐阅读