我有一个名为df_cp的数据框,它的数据如下,

我需要在索引为1的行中的第一个空单元格中插入CompanyID 'LCM‘的新项目名称。我已经找到了我感兴趣的行的索引,
index_row = df_cp[df_cp['CompanyID']=='LCM'].index如何在index_row为1的行内迭代,任务是将索引1处的第一个NaN替换为"Healthcare“。
请帮帮忙。
发布于 2020-05-25 02:38:21
IIUC,你可以使用isna和idxmax
df.loc[1, df.loc[1].isna().idxmax()] = 'Healthcare'输出:
CompanyID Project01 Project02 Project03 Project04 Project05
0 134 oil furniture NaN NaN NaN
1 LCM oil furniture car Healthcare NaN
2 Z01 oil furniture NaN NaN NaN
3 453 oil furniture agro meat NaN注意: idxmax返回第一次出现最大值的索引。
更一般地说:
m = df['CompanyID'] == 'LCM'
df.loc[m, df[m].isna().idxmax(axis=1)] = 'Healthcare'
df输出:
CompanyID Project01 Project02 Project03 Project04 Project05
0 134 oil furniture NaN NaN NaN
1 LCM oil furniture car Healthcare NaN
2 Z01 oil furniture NaN NaN NaN
3 453 oil furniture agro meat NaNhttps://stackoverflow.com/questions/61990435
复制相似问题