Skip to content Skip to sidebar Skip to footer

How To Replace Values In A Column If Another Column Is A Nan?

So this should be the easiest thing on earth. Pseudocode: Replace column C with NaN if column E is NaN I know I can do this by pulling out all dataframe rows where column E is NaN

Solution 1:

Use np.where:

In [34]:
dfz['C'] = np.where(dfz['E'].isnull(), dfz['E'], dfz['C'])
dfz

Out[34]:
   A  B   C  D    E
01111221000015200 NaN  0  NaN
3111110400 NaN  0  NaN
50110557

Or simply mask the df:

In [38]:
dfz.loc[dfz['E'].isnull(), 'C'] = dfz['E']
dfz

Out[38]:
   A  B   C  D    E
01111221000015200 NaN  0  NaN
3111110400 NaN  0  NaN
50110557

Post a Comment for "How To Replace Values In A Column If Another Column Is A Nan?"