Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: drop value=0 row in specific columns [duplicate]

I want to drop rows with zero value in specific columns

>>> df

   salary   age   gender
0   10000    23     1
1   15000    34     0
2   23000    21     1
3     0      20     0
4   28500     0     1
5   35000    37     1

some data in columns salary and age are missing and the third column, gender is a binary variables, which 1 means male 0 means female. And 0 here is not a missing data, I want to drop the row in either salary or age is missing so I can get

>>> df
   salary   age   gender
0   10000    23     1
1   15000    34     0
2   23000    21     1
3   35000    37     1
like image 862
蔡嚴毅 Avatar asked Jan 03 '23 13:01

蔡嚴毅


1 Answers

Option 1

You can filter your dataframe using pd.DataFrame.loc:

df = df.loc[~((df['salary'] == 0) | (df['age'] == 0))]

Option 2

Or a smarter way to implement your logic:

df = df.loc[df['salary'] * df['age'] != 0]

This works because if either salary or age are 0, their product will also be 0.

Option 3

The following method can be easily extended to several columns:

df.loc[(df[['a', 'b']] != 0).all(axis=1)]

Explanation

  • In all 3 cases, Boolean arrays are generated which are used to index your dataframe.
  • All these methods can be further optimised by using numpy representation, e.g. df['salary'].values.
like image 50
jpp Avatar answered Jan 05 '23 17:01

jpp