In a python script using the library pandas
, I have a dataset of let's say 100 lines with a feature "X", containing 36 NaN
values, and a list of size 36.
I want to replace all the 36 missing values of the column "X" by the 36 values I have in my list.
It's likely to be a dumb question, but I went through all the doc and couldn't find a way to do it.
Here's an example :
INPUT
Data: X Y
1 8
2 3
NaN 2
NaN 7
1 2
NaN 2
Filler
List: [8, 6, 3]
OUTPUT
Data: X Y
1 8
2 3
8 2
6 7
1 2
3 2
By using dropna() method you can drop rows with NaN (Not a Number) and None values from pandas DataFrame. Note that by default it returns the copy of the DataFrame after removing rows. If you wanted to remove from the existing DataFrame, you should use inplace=True .
Start with your dataframe df
print(df)
X Y
0 1.0 8
1 2.0 3
2 NaN 2
3 NaN 7
4 1.0 2
5 NaN 2
Define the values you want to fill with (Note: there must be the same number of elements in your filler
list as NaN
values in your dataframe)
filler = [8, 6, 3]
Filter your column (that contains the NaN
values) and overwrite the selected rows with your filler
df.X[df.X.isnull()] = filler
df.loc[df.X.isnull(), 'X'] = filler
which gives:
print(df)
X Y
0 1.0 8
1 2.0 3
2 8.0 2
3 6.0 7
4 1.0 2
5 3.0 2
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With