Pandas数据框架 – 所有列的逻辑或

huangapple go评论102阅读模式
英文:

Pandas Data Frame - logical or of all columns

问题

A B C OR
True True False True
False True False True
False False False False
英文:

I have a dynamically created data frame which contains, multiple columns with True/False values. Let's say that it looks like this:

A B C
True True False
False True False
False False False

I need to create a column which values will be a result of logical or on the rest of the columns.

The output would look like this:

A B C OR
True True False True
False True False True
False False False False

答案1

得分: 1

使用 DataFrame.any

df['OR'] = df.any(axis=1)

如果需要仅筛选某些列:

cols = ['A', 'B']
df['OR'] = df[cols].any(axis=1)
英文:

Use DataFrame.any:

df['OR'] = df.any(axis=1)

If need filter only some columns:

cols = ['A','B']
df['OR'] = df[cols].any(axis=1)

huangapple
  • 本文由 发表于 2023年3月8日 17:45:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/75671464.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定