I have a dataframe looks like:
SK_ID_CURR CREDIT_ACTIVE
0 215354 Closed
1 215354 Active
2 215354 Active
3 215354 Active
4 215354 Active
5
You can use pandas.crosstab, which avoids your suggested intermediary step:
res = pd.crosstab(df['SK_ID_CURR'], df['CREDIT_ACTIVE'])
print(res)
CREDIT_ACTIVE Active Closed
SK_ID_CURR
162297 1 2
215354 6 1
You can achieve this using pd.DataFrame.groupby
df1.groupby(['SK_ID_CURR','CREDIT_ACTIVE']).size()
Output:
SK_ID_CURR CREDIT_ACTIVE
162297 Active 1
Closed 2
215354 Active 6
Closed 1