1

For example, I have my data in the following form:

    Group Product
0      1       A
1      1       A
2      1       B
3      2       A
4      2       B
5      2       C
6      3       A
7      3       C
8      3       C

What I would like to achieve is having it be like the following:

   Group  A  B  C
0      1  2  1  0
1      2  1  1  1
2      3  1  0  1

Where values of A, B and C columns are respectively their occurences for the given group.

How can I achieve this using pandas?

I tried using groupby and count with the following code

df.groupby(['Group','Product'])['Product'].count()

Which provided me with the results I wanted however I have no idea how to put them into seperate columns for each count.

2 Answers2

2

You can use pd.crosstab:

x = pd.crosstab(df['Group'], df['Product']).reset_index()
x.columns.name = None

print(x)

Prints:

   Group  A  B  C
0      1  2  1  0
1      2  1  1  1
2      3  1  0  2
Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91
1

Pandas pivot_table should do it.

pivot_df = pd.pivot_table(df, index='Group', columns='Product', aggfunc=len, fill_value=0)

pivot_df = pivot_df.reset_index()
pivot_df.columns.name = None

print(pivot_df)
Mulloy
  • 156
  • 1
  • 8