If I have a Pandas data frame like this:
0 20 30 40 50
1 5 NaN 3 5 NaN
2 2 3 4 NaN 4
3 6 1 3 1 NaN
How do I replace each value with its column value such that I get a pandas data frame like this:
0 20 30 40 50
1 0 NaN 30 40 NaN
2 0 20 30 NaN 50
3 0 20 30 40 NaN
IIUC using mul
df.notnull().mul(df.columns,1).replace('',np.nan)
0 20 30 40 50
1 0 NaN 30 40 NaN
2 0 20 30 NaN 50
3 0 20 30 40 NaN
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.