What should I do to solve my problem with dropna and fillna in python?

Question:

I see there are some missing data, in my data.
there are NaN values in “Type 2” column.

you see NaN values here

When i write this code to drop the rows which have NaN values;

its after using dropna

When i write this code to add “empty” which have NaN values;

and this one ater using fillna

I restarted my jupyter notebook but its still the same.

Asked By: user10835338

||

Answers:

you can use:

data = data[data['Type 2'].notna()]
Answered By: kederrac

Try

data.dropna(axis=1, inplace=True)

axis=1 means columns axis=0 means rows
if any column contains NaN it will remove entire column similarly row

Answered By: Mannya

When you do data['Type 2'].dropna(inplace=True), that’s creating a new series (for your “Type 2” column) and dropping the nans from that new data structure. It doesn’t change your original dataframe.

If you want to dropna from the DataFrame, you need to do it directly on that object, not on the column object. The following drops nans directly on the DataFrame:

data.dropna(subset=['Type 2'], inplace=True)

Note that here we’re calling dropna on data directly. subset is the argument that you can use to tell pandas which columns to use to determine which rows to drop.

Answered By: metaperture

you can try this one to fill your data

data = data.ffill().bfill()
Answered By: Nielio
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.