1

Suppose we have the following df:

import pandas as pd
data = {"Team": ["Red Sox", "Red Sox", "Red Sox", "Red Sox", "Red Sox", "Red Sox", "Yankees", "Yankees", "Yankees", "Yankees", "Yankees", "Yankees"],
        "Pos": ["Pitcher", "Pitcher", "Pitcher", "Not Pitcher", "Not Pitcher", "Not Pitcher", "Pitcher", "Pitcher", "Pitcher", "Not Pitcher", "Not Pitcher", "Not Pitcher"],
        "Age": [24, 28, 40, 22, 29, 33, 31, 26, 21, 36, 25, 31]}
df = pd.DataFrame(data)

When I groupby two keys Team and Pos what I get is:

print(df.groupby(['Team','Pos'])['Age'].count())

      Team          Pos  Age
0  Red Sox  Not Pitcher    3
1  Red Sox      Pitcher    3
2  Yankees  Not Pitcher    3
3  Yankees      Pitcher    3

I would like to have levels of Pos index into columns as such:

Team Not_Pitcher Pitcher
Red Sox 3 3
Yankees 3 3
I'mahdi
  • 23,382
  • 5
  • 22
  • 30
moth
  • 1,833
  • 12
  • 29

2 Answers2

2

Try pd.crosstab

pd.crosstab(df.Team,df.Pos)
Pos      Not Pitcher  Pitcher
Team                         
Red Sox            3        3
Yankees            3        3
BENY
  • 317,841
  • 20
  • 164
  • 234
1

You can after groupby convert result to DataFrame then use pandas.pivot_table like below:

>>> res_df = df.groupby(['Team','Pos'])['Age'].count().reset_index(name= 'Count')
>>> res_df
      Team          Pos  Count
0  Red Sox  Not Pitcher      3
1  Red Sox      Pitcher      3
2  Yankees  Not Pitcher      3
3  Yankees      Pitcher      3

>>> res_df.pivot_table(index='Team', columns='Pos', values='Count')

Pos      Not Pitcher  Pitcher
Team                         
Red Sox            3        3
Yankees            3        3

By thanks @HenryYik the shortest answer can be:

>>> df.groupby(['Team', 'Pos']).size().unstack("Pos")
I'mahdi
  • 23,382
  • 5
  • 22
  • 30