I am getting following warning:
SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame
on following line:
df3[['prod_tags_0', 'prod_tags_1', 'prod_tags_2', 'prod_tags_3']].fillna(0, inplace=True)
How I can solve this warning ?
df3[['prod_tags_0', 'prod_tags_1', 'prod_tags_2', 'prod_tags_3']]
creates a new dataframe which is a subset of df3
. Since you are using inplace=True
you are getting the aforementioned warning since it tries to modify the new dataframe inplace, to which you don't keep a reference around (and I suspect that if you'd print df3
you will see that this line actually had no effect).
I'd do one of the following:
re-assign the newly created dataframe back to df3
without using inplace=True
:
df3[['prod_tags_0', 'prod_tags_1', 'prod_tags_2', 'prod_tags_3']] = \
df3[['prod_tags_0', 'prod_tags_1', 'prod_tags_2', 'prod_tags_3']].fillna(0)
Or the slightly preferable way (in my opinion at least):
Pass fillna
the columns that you want to modify as a dictionary. The keys are the columns' names and the values are what NaN
should be replaced with in each column:
df3.fillna({'prod_tags_0': 0, 'prod_tags_1': 0,
'prod_tags_2': 0, 'prod_tags_3': 0}, inplace=True)
In this example you can use dictionary comprehension:
import pandas as pd
import numpy as np
df = pd.DataFrame({'prod_tags_0': [np.nan], 'prod_tags_1': [np.nan],
'prod_tags_2': [np.nan]})
print(df)
>> prod_tags_0 prod_tags_1 prod_tags_2
0 NaN NaN NaN
df.fillna({'prod_tags_' + str(i): 0 for i in range(2)}, inplace=True)
print(df)
>> prod_tags_0 prod_tags_1 prod_tags_2
0 0.0 0.0 NaN
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