如何在没有 if 语句的情况下通过使用 True 和 False 检查多列来 return 一列
How to return a column by checking multiple column with True and False without if statements
如何在不使用 if 语句的情况下获得所需的输出?并逐行检查
import pandas as pd
test = pd.DataFrame()
test['column1'] = [True, True, False]
test['column2']= [False,True,False]
index column1 column2
0 True False
1 True True
2 False False
desired output:
index column1 column2 column3
0 True False False
1 True True True
2 False False False
非常感谢您的帮助。
提前致谢。
使用 DataFrame.all
测试所有值是否为 True
s:
test['column3'] = test.all(axis=1)
如果需要筛选列添加子集 ['column1','column1']
:
test['column3'] = test[['column1','column1']].all(axis=1)
如果只想测试 2 列,可以使用 &
按位 AND
:
test['column3'] = test['column1'] & test['column1']
如何在不使用 if 语句的情况下获得所需的输出?并逐行检查
import pandas as pd
test = pd.DataFrame()
test['column1'] = [True, True, False]
test['column2']= [False,True,False]
index column1 column2
0 True False
1 True True
2 False False
desired output:
index column1 column2 column3
0 True False False
1 True True True
2 False False False
非常感谢您的帮助。
提前致谢。
使用 DataFrame.all
测试所有值是否为 True
s:
test['column3'] = test.all(axis=1)
如果需要筛选列添加子集 ['column1','column1']
:
test['column3'] = test[['column1','column1']].all(axis=1)
如果只想测试 2 列,可以使用 &
按位 AND
:
test['column3'] = test['column1'] & test['column1']