Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to drop null values in Pandas? [duplicate]

Tags:

python

pandas

I try to drop null values of column 'Age' in dataframe, which consists of float values, but it doesn't work. I tried

data.dropna(subset=['Age'], how='all')
data['Age'] = data['Age'].dropna()
data=data.dropna(axis=1,how='all')

It works for other columns but not for 'Age'

    Pclass  Fare    Age Sex
0   3   7.2500  22.0    1
1   1   71.2833 38.0    0
2   3   7.9250  26.0    0
3   1   53.1000 35.0    0
4   3   8.0500  35.0    1
5   3   8.4583  NaN 1
6   1   51.8625 54.0    1
7   3   21.0750 2.0 1
like image 628
Alex Ermolaev Avatar asked Sep 11 '16 12:09

Alex Ermolaev


People also ask

How do I drop duplicates in Pandas?

Remove All Duplicate Rows from Pandas DataFrame You can set 'keep=False' in the drop_duplicates() function to remove all the duplicate rows. For E.x, df. drop_duplicates(keep=False) .

What does Drop_duplicates do in Pandas?

The drop_duplicates() method removes duplicate rows. Use the subset parameter if only some specified columns should be considered when looking for duplicates.


1 Answers

data.dropna(subset=['Age']) would work, but you should either set inplace=True or assign it back to data:

data = data.dropna(subset=['Age'])

or

data.dropna(subset=['Age'], inplace=True)
like image 64
DeepSpace Avatar answered Sep 22 '22 19:09

DeepSpace