简体   繁体   English

Python DataFrame:基于另一列的出现次数

[英]Python DataFrame: count of occurances based on another column

I have a Python Data Frame of teams and a place that they have achieved (1, 2 or 3)我有一个 Python 团队数据框和他们取得的成绩(1、2 或 3)

Team团队 place地方
A一个 1 1
A一个 1 1
A一个 1 1
A一个 2 2
A一个 3 3
A一个 1 1
A一个 1 1
B 2 2
B 2 2

I want to manipulate the df to look like this below.我想操纵 df 看起来像下面这样。 So it is a count of how often a team has achieved each place.所以这是一个团队达到每个地方的频率。

Team团队 1 1 2 2 3 3
A一个 5 5 1 1 1 1
B 0 0 2 2 0 0

You can get the value counts for each group and then unstack the index.您可以获取每个组的值计数,然后取消堆叠索引。 The rest is twiddling to get your exact output. rest 正在转动以获得您的确切 output。

(df.groupby('Team')['place']
   .value_counts()
   .unstack(fill_value=0)
   .reset_index()
   .rename_axis(None, axis=1)
) 

You could use pandas.crosstab :您可以使用pandas.crosstab

pd.crosstab(df['Team'], df['place'])

or a simple groupby + size and unstack :或简单的groupby + sizeunstack

(df.groupby(['Team', 'place']).size()
   .unstack('place', fill_value=0)
)

output: output:

place  1  2  3
Team          
A      5  1  1
B      0  2  0
all as columns全部作为列
(pd.crosstab(df['Team'], df['place'])
   .rename_axis(columns=None)
   .reset_index()
)

output: output:

  Team  1  2  3
0   A   5  1  1
1   B   0  2  0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM