You can select your desired columns and do it by assignment:
df[['a', 'b']] = df[['a','b']].fillna(value=0)
The resulting output is as expected:
a b c
0 1.0 4.0 NaN
1 2.0 5.0 NaN
2 3.0 0.0 7.0
3 0.0 6.0 8.0
Answer from root on Stack OverflowYou can select your desired columns and do it by assignment:
df[['a', 'b']] = df[['a','b']].fillna(value=0)
The resulting output is as expected:
a b c
0 1.0 4.0 NaN
1 2.0 5.0 NaN
2 3.0 0.0 7.0
3 0.0 6.0 8.0
You can using dict , fillna with different value for different column
df.fillna({'a':0,'b':0})
Out[829]:
a b c
0 1.0 4.0 NaN
1 2.0 5.0 NaN
2 3.0 0.0 7.0
3 0.0 6.0 8.0
After assign it back
df=df.fillna({'a':0,'b':0})
df
Out[831]:
a b c
0 1.0 4.0 NaN
1 2.0 5.0 NaN
2 3.0 0.0 7.0
3 0.0 6.0 8.0
Pandas conditional fillna based on another column values
I finally solved in this way :
missing = train["Outlet_Size"].isna()
condlist = [train.loc[missing, "Outlet_Size"] & train.loc[missing,'Item_Outlet_Sales'] <= 1000,
train.loc[missing, "Outlet_Size"] & train.loc[missing,'Item_Outlet_Sales'] > 1000]
choicelist = ["Small", "Medium"]
train.loc[missing, 'Outlet_Size'] = np.select(condlist, choicelist)
How do I fill NaN values of a column with its mean/median/mode? (Pandas DataFrame)
How can I replace all NaN values in my DF except the NaN values of some specific columns?
Pandas: Is it possible to use the fillna() method using a calculation between two columns of a specific row?
Videos
Hello,
I am working on bigmart dataset and I would like to substitute missing values of a column based on the values of another column, practically:
| Outlet_Size | sales_bin |
|---|---|
| Medium | 3000-4000 |
| NaN | 0-1000 |
| Small | 0-1000 |
| .... | .... |
So if train[“Outlet_Size”] value is a NaN and train[“sales_bin”] is “0-1000”
train[“Outlet_Size”] value shoud become “Small”
else == Medium
But I really don’t know how to write it and all the information I found seems confusing to me
Is it possible to do it? How?
Many thanks
I finally solved in this way :
missing = train["Outlet_Size"].isna()
condlist = [train.loc[missing, "Outlet_Size"] & train.loc[missing,'Item_Outlet_Sales'] <= 1000,
train.loc[missing, "Outlet_Size"] & train.loc[missing,'Item_Outlet_Sales'] > 1000]
choicelist = ["Small", "Medium"]
train.loc[missing, 'Outlet_Size'] = np.select(condlist, choicelist)
df.loc[(df.col1.isna()) & (df.col2 == '0-1000'), 'col1'] = 'small'.
Try something like this
Hello everyone, i'm trying to fill the missing values in a specific column of a dataframe, and i want to fill them using the mean, median or mode. How can i do it using pandas or numpy?