0

I have a dataframe like this:

import pandas as pd

df1 = pd.DataFrame(
{'A': {0: '1',  1: '7',  2: '7',  3: '1',  4: '7',  5: '1'},
 'B': {0: 'S', 1: 'S', 2: 'D', 3: 'D', 4: 'D', 5: 'S'}})
display(df1.head())

I want to convert above dataframe (dynamically) to this:

df2 = pd.DataFrame({
'A': {0: '1',  1: '7'},
'S': {0: 2, 1: 1},
'D': {0: 1, 1: 2},
})
display(df2.head())

enter image description here enter image description here

I know how to use 'groupBy', but I don't know how to automatically convert fields into new columns.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Mahdi Shad
  • 1,427
  • 1
  • 14
  • 23
  • @jezrael This is not my problem. In my case, It may be 5 columns or more. I want this number of columns to be added automatically – Mahdi Shad Jul 06 '22 at 08:36
  • 1
    OOps, you are right, need `pd.crosstab(df1.A, df1.B).rename_axis(None, axis=1).reset_index()` – jezrael Jul 06 '22 at 08:38
  • @jezrael Thank you but I don't understand your answer. I need something like this: df.groupBy(['A'])['B'].countAndConvert(). Can you post complete answer to my question ? – Mahdi Shad Jul 06 '22 at 14:00
  • 1
    use `df.groupBy(['A'])['B'].size(). unstack(fill_value=0)` – jezrael Jul 06 '22 at 17:08

0 Answers0