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: Is it possible to use the fillna() method using a calculation between two columns of a specific row?
How can I replace all NaN values in my DF except the NaN values of some specific columns?
I need to replace NaN in one column with value for other col
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)
Videos
I am currently cleaning data using Pandas of bike sales.
Each bike sale is broken down into: 'Quantity_Sold' 'Total_Cost', 'Total_Revenue', 'Total_Profit' , 'Unit_Cost', 'Unit_Price', 'Unit_Profit'.
There are Null values for some of these columns, however, it is possible to calculate the missing column's valuing using the other remaining columns that are filled. For example a null "Total_Cost" column can be calculated via "Unit_Cost"*"Quantity" etc.
How do I use the fillna() method to do this, so I can fill in the columns without resorting to mean, median and averages?