I have created this pandas dataframe:
import pandas as pd
import numpy as np
ds = {'col1':['A1','A1','A1','B1','B2','C1'],'col2':['CCJ','CCJ','AAA','CCJ','CCJ','OOO']}
df = pd.DataFrame(data=ds)
which looks like this:
print(df)
col1 col2
0 A1 CCJ
1 A1 CCJ
2 A1 AAA
3 B1 CCJ
4 B2 CCJ
5 C1 OOO
I need to get the following dataframe that is grouped by col1
and contains three features (one feature for each unique value in col2
) that count the number of occurrences of the values in col2
. So, the output dataframe would look like this:
So, for example, let's consider, in the latest dataframe, the row for A1
:
- how many CCJ are there for A1? There are 2. Hence #_CCJ = 2
- how many AAA are there for A1? There is 1. Hence #_AAA = 1
- how many OOO are there for A1? There are none. Hence #_OOO = 0
And so on.
Can anyone help me with this please?