英文:
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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论