我有其他程序,我分组和计数领域。现在,我想得到每个布尔字段的计数。有没有一种方法比我循环编写自己的代码更好呢?理想情况下,我会用结果生成一个新的数据帧(有点像我做的那样)
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