我想用相应的列名填充我的数据表的非空值。例如:
df = pd.DataFrame({'Time': ['2000-01-01 00:00:10.870', '2000-01-03 00:00:08.160','2000-01-03 00:00:10.870', '2000-01-06 00:00:11.160'],
'E1 ':[1 ,np.nan, 1, 1],'E2 ':[1 ,np.nan, 1, 1]})
E1 E2 Time
0 1.0 1.0 2000-01-01 00:00:10.870
1 2000-01-03 00:00:08.160
2 1.0 1.0 2000-01-03 00:00:10.870
3 1.0 1.0 2000-01-06 00:00:11.160
渴望出局:
E1 E2 Time
0 E1 E2 2000-01-01 00:00:10.870
1 2000-01-03 00:00:08.160
2 E1 E2 2000-01-03 00:00:10.870
3 E1 E2 2000-01-06 00:00:11.160
提前致谢
答案 0 :(得分:1)
这是一种方法:
for col in ['E1 ', 'E2 ']:
df.loc[df[col].notnull(), col] = col
如果您想要包含更多列,则可以只列出这些列名称,如下所示:
cols_to_fill = ['col_x', 'col_y', 'col_z', ...]
for col in cols_to_fill:
df.loc[df[col].notnull(), col] = col]
或者,要对数据框中的所有列执行此操作:
for col in df.columns:
df.loc[df[col].notnull(), col] = col]