给出这样一个完全分类的数据框
import pandas as pd
data = {"Name": ["Dakota", "Joe", "Dakota", "Alex", "Dakota"],
"Activities":["Chess", "Bowling", "Chess", "Swimming", "Chess"],
"Gender": ["Male", "Male", "Male", "Female", "Female"]}
df = pd.DataFrame.from_dict(data)
我想计算出每行出现类似次数的次数
Dakota Chess Male: 2
Dakota Chess Female: 1
Joe Bowling Male: 1
Alex Female Swimming: 1
答案 0 :(得分:1)
我认为您需要使用现有数据框中的数据进行所有排列。在此处查看答案:Quick way to find all permutations of a pandas DataFrame
祝你好运!
答案 1 :(得分:1)
df.groupby(by = ['Name','Activities','Gender'])['Gender']。count()
姓名活动性别
亚历克斯游泳女1
达科他象棋女1 男2
乔保龄球男1
答案 2 :(得分:1)
您还可以将所有行转换为列表列表,然后使用集合中的计数器。
allValues=df.values.tolist()
rows = []
for row in allValues:
rows.append(' '.join(row))
print(Counter(rows))
输出:
柜台({'达科他国际象棋男:2,'乔保龄球男':1,'亚历克斯游泳女':1,'达科他国际象棋女':1})