What should I do to solve my problem with dropna and fillna in python?
Question:
Answers:
you can use:
data = data[data['Type 2'].notna()]
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
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.
you can try this one to fill your data
data = data.ffill().bfill()
you can use:
data = data[data['Type 2'].notna()]
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
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.
you can try this one to fill your data
data = data.ffill().bfill()