Please explain, why this won't work (hc is pandas dataframe on below example):
import pandas as pd
import numpy as np
hc = pd.DataFrame([['Adolf', np.nan],
['Hans', 'Johan']],
columns=('First Name', 'Second Name'))
hc.fillna(value=hc["First Name"])
First Name Second Name 0 Adolf NaN 1 Hans Johan
hc[['First Name','Second Name']].fillna(value=hc["First Name"])
First Name Second Name 0 Adolf NaN 1 Hans Johan
Using other column's value works only for one column like that:
hc['Second Name'].fillna(value=hc["First Name"],inplace=True)
hc
First Name Second Name 0 Adolf Adolf 1 Hans Johan
I wish to apply a value (mask value) from one column to NaN-s in the entire dataframe.
CodePudding user response:
IIUC, you want to fillna
with a Series as reference, per row.
hc = hc.fillna(hc['First Name'])
This is not currently supported on the columns for all pandas version (it is in the latest), but you can cheat using a double transpose to perform the operation on the rows:
hc = hc.T.fillna(hc['First Name']).T
output:
First Name Second Name
0 Adolf Adolf
1 Hans Johan