I have a dataframe like below:
id | name | colA | colB |
---|---|---|---|
One | Ana | abc | xyz |
One | Ana | abc | xyz |
One | Ana | bde | xyz |
One | Ana | bde | xyz |
One | Ana | bde | yhn |
One | Ana | bde | yhn |
One | Ana | bde | qwe |
One | Ana | teh | qwe |
Two | Bob | abc | qwe |
Two | Bob | teh | qwe |
Two | Bob | pop | omg |
I need to transform my dataframe as
id | name | abc | bde | teh | pop | xyz | yhn | qwe | omg |
---|---|---|---|---|---|---|---|---|---|
One | Ana | 2 | 5 | 1 | 0 | 4 | 2 | 2 | 0 |
Two | Bob | 1 | 0 | 1 | 1 | 0 | 0 | 2 | 1 |
I wrote below code to achieve this but it do not gives me expected output and also I have no idea how to perform it for multiple columns. Please help.
df = df.groupby(['id','colA']).size().reset_index(name='colA_counts')