8
$\begingroup$

I'd like to drop all the rows containing a NaN values pertaining to a column. Lets assume I have a dataset like this:

Age  Height  Weight  Gender
12    5'7     NaN     M
NaN   5'8     160     M
32    5'5     165     NaN
21    NaN     155     F
55    5'10    170     NaN

I want to remove all the rows where 'Gender' has NaN values. The output i'd like:

Age  Height  Weight Gender
12   5'7     NaN     M
NaN  5'8     160     M
21   NaN     155     F

Thanks in advance!

$\endgroup$
1
  • 2
    $\begingroup$ You really have to tell us what data structure it is in. E.g. is it SQL? $\endgroup$ Apr 13, 2018 at 1:32

2 Answers 2

15
$\begingroup$

Well if the dataset is not too large I would suggest using pandas to clean the data. So you would need to first do

Python2

python2 -m pip install pandas

Python3

python3 -m pip install pandas

If you already have anaconda installed you can skip the above step. Next you could go through an IDE (like jupyter) or through the shell type the following commands

import pandas as pd
df = pd.read_csv("filename", dtype=str)
#or if excel file
#df = pd.read_excel("filename", dtype=str)

df = df[pd.notnull(df['Gender'])]

Then you would want to save your result in a file with

df.to_csv("newfile"); 
$\endgroup$
5
$\begingroup$

If you are working on Python Dataframe, Please try this.

import pandas as pd
df = df[np.isfinite(df['Gender'])]

Or you can use notnull instead of isfinite.

import pandas as pd
df = df[pd.notnull(df['Gender'])]
$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.