0

My data looks smth like:

Index Job Y Balance
1 A Yes 1
2 B No 2
3 A No 5
4 A No 0
5 B Yes 4

I want to summarize the data in the following format, with job in the row and Y in the column:

Yes No
A 1 2
B 1 1

I have tried the following code:

pivot = df.pivot_table(index =['job'], columns = ['y'], values = ['balance'], aggfunc ='count')

I am not able to run the pivot without using balance in the value parameter. How do I get the above result?

1 Answers1

0

Use pd.crosstab:

res = pd.crosstab(df.Job, df.Y)

res

Y    No  Yes
Job         
A     2    1
B     1    1

# to get rid of the names for the columns (`Y`) and index (`Job`):
res.columns.name = None
res.index.name = None

res

   No  Yes
A   2    1
B   1    1
ouroboros1
  • 9,113
  • 3
  • 7
  • 26