我已经创建了一个 DataFrame,现在需要计算每个重复的行(例如 df['Gender']。假设 Gender 'Male' 出现两次,女性出现 3 次,我需要制作此列:
Gender Occurrence
Male 1
Male 2
Female 1
Female 2
Female 3
有没有办法用 Pandas 做到这一点?
最佳答案
按 Gender
分组后使用 cumcount 方法:
df = pd.DataFrame({'Gender':['Male','Male','Female','Female','Female']})
df['Occurrence'] = df.groupby('Gender').cumcount() + 1
print(df)
Gender Occurrence
0 Male 1
1 Male 2
2 Female 1
3 Female 2
4 Female 3
计数从 0 开始,所以我在那里添加了一个
+ 1
。关于python - Pandas DataFrame 计算重复行并填充列,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43015345/