Skip to main content
 首页 » 编程设计

python之根据另一列的值在 Pandas 中创建新列

2024年11月01日6java哥

任务如下:

Add a new column to df called income10. It should contain the same values as income with all 0 values replaced with 1.

我试过下面的代码:

df['income10'] = np.where(df['income']==0, df['income10'],1) 

但我一直收到错误:

请您参考如下方法:

您可以对列中的每个值应用一个函数:

df["a"] = df.a.apply(lambda x: 1 if x == 0 else x)