gpt4 book ai didi

python - pandas 根据之前的列生成新的列

转载 作者:行者123 更新时间:2023-12-01 03:02:39 27 4
gpt4 key购买 nike

有一个数据集,其中一列包含一些缺失值。我想生成一个新列,如果前一列的单元格缺失,则为新列分配 1,否则为 0。

我试过了

df[newcolumn] = map(lamba x: 1 if x is None else 0, df[formercolumn])
但没有成功。

同时

df[newcolunm] = df[formercolunms].isnull().apply(lambda x: 1 if x is True else 0)

效果很好。

针对这种情况有更好的解决方案吗?

最佳答案

使用np.where:

df['newcolumns'] = np.where(df.formercolumns.isnull(),0,1)

关于python - pandas 根据之前的列生成新的列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43670825/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com