首页 > 解决方案 > 如何计算多系列中的真数?

问题描述

我有多个系列如下

import pandas as pd

ser1 = pd.Series([True, True, False, True, False], index=['John', 'Alice', 'Lisa', 'Flank', 'King'])
ser2 = pd.Series([True, False, False, False, True], index=['John', 'Alice', 'Lisa', 'Flank', 'King'])
ser3 = pd.Series([False, True, False, False, True], index=['John', 'Alice', 'Lisa', 'Flank', 'King'])

我想计算每一行的真数,例如约翰有两个真结果丽莎得到零。有人知道怎么做吗?

标签: pythonpandas

解决方案


让我们做pd.concat

pd.concat([ser1,ser2,ser3],axis=1).sum(axis=1)
John     2
Alice    2
Lisa     0
Flank    1
King     2
dtype: int64

推荐阅读