1

I have a Python Data Frame of teams and a place that they have achieved (1, 2 or 3)

Team place
A 1
A 1
A 1
A 2
A 3
A 1
A 1
B 2
B 2

I want to manipulate the df to look like this below. So it is a count of how often a team has achieved each place.

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

2 Answers 2

1

You could use pandas.crosstab:

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

or a simple groupby+size and unstack:

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

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:

  Team  1  2  3
0   A   5  1  1
1   B   0  2  0
Sign up to request clarification or add additional context in comments.

Comments

1

You can get the value counts for each group and then unstack the index. The rest is twiddling to get your exact output.

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

1 Comment

I realized while posting that this is in fact a simple crosstab :p

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.