【问题标题】:Create a new column in Pandas Dataframe based on the 'NaN' values in other columns根据其他列中的“NaN”值在 Pandas Dataframe 中创建一个新列
【发布时间】:2025-12-12 10:20:09
【问题描述】:

根据单独列中的 nan 值创建新列的最有效方法是什么(考虑到数据框非常大) 在OTW中,如果任何一列在其中一行中有NaN,那么新列的对应值应该是1

注意:列的 dtypes 可能是不同的对象,而不仅仅是整数/浮点数

X A   B
1 2   3    
4 NaN 1    
7 8   9    
3 2   NaN  
5 NaN 2   

应该给

X A   B    C
1 2   3    0
4 NaN 1    1
7 8   9    0
3 2   NaN  1
5 NaN 2    1

代码已尝试(感谢一些在线帮助):

df['C'] = np.where(np.any(np.isnan(df[['A', 'B']])), 1, 0)

但它会引发以下错误

TypeError: ufunc 'isnan' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

这会返回一个空数据框(因为 A 和 B 列在单行中从来没有 NaN 值

df['C'] = np.where(np.any(pd.isnull(df[['A', 'B']])), 1, 0)

找到解决方法:

df['C1'] = np.where(np.isnan(df['A'].values), 1, 0) 
df['C2'] = np.where(np.isnan(df['B'].values), 1, 0)
df['C'] = df[['C1','C2']].max(axis=1)

然后您可以删除C1C2

希望对你有帮助~

【问题讨论】:

  • 试过了,返回一个空的数据框。我认为它在上面的 sn-p 中使用了 AND 运算符,它应该使用 OR
  • 您的 df 中没有 A 列和 B 列
  • 请检查新的编辑@WeNYoBen
  • 数据类型是什么?
  • 在我当前的用例中,日期时间

标签: python pandas numpy dataframe


【解决方案1】:

这比你想象的要简单。希望这可以帮助你!

df['C'] = df.isna().sum(axis=1).apply(lambda x: 0 if x==0 else 1)

【讨论】:

  • 如何使用这个 sn-p 指定列?我只想检查特定列
  • 你可以使用 df[['A','B','C']].isna().sum(axis=1).apply(lambda x: 0 if x==0否则 1)
  • 你的意思是df['C'] = df[['A','B']].isna().sum(axis=1).apply(lambda x: 0 if x==0 else 1)?这不起作用,返回一个空的df
  • 请检查所有相关代码。它运行成功。参考链接paste.pics/be8af44c7f5f3afdc37a23e9dd32e192
  • 更新了问题。 A 和 B 的 dtypes 不必是整数。可能是日期时间之类的对象
【解决方案2】:

您在any 中缺少axis=1

np.where(np.any(np.isnan(df[['A', 'B']]),axis=1), 1, 0)
Out[80]: array([0, 1, 0, 1, 1])

【讨论】:

  • np.isnan 仍然给出问题中提到的类型错误
最近更新 更多