【发布时间】:2021-02-25 10:04:41
【问题描述】:
我还有其他程序可以对字段进行分组和计数。现在,我想计算每个布尔字段。有没有一种 Pandas 方法可以做到这一点,而不是我循环和编写自己的代码?理想情况下,我会生成一个带有结果的新数据框(有点像我所做的here)。
简单示例 CSV 数据(生成的扑克手数据):
Hand,Other1,Other2,IsFourOfAKind,IsThreeOfAKind,IsPair
1,'a','b',1,0,0
2,'c','d',0,1,0
3,'a','b',0,1,0
4,'x','y',0,0,1
5,'a','b',0,0,1
6,'a','b',0,0,1
7,'a','b',0,0,1
计划:
import pandas as pd
import warnings
filename = "./data/TestGroup2.csv"
# tell run time to ignore certain read_csv type errors (from pandas)
warnings.filterwarnings('ignore', message="^Columns.*")
count_cols = ['IsFourOfAKind','IsThreeOfAKind','IsPair ']
enter code here
#TODO - use the above to get counts of only these columns
df = pd.read_csv(filename)
print(df.head(10))
所需的输出 - 可能只是一个新的数据框
Column Count
IsFourOfAKind 1
IsThreeOfAKind 2
IsPair 3
【问题讨论】: