Reputation: 493
I a have data set like this
df = pd.DataFrame({"col_1" :["A","B","B","B","A"], "col_2": ["JK","ML","ML","JK","JK"]})
So I want to find percentage of these two columns. example I want to know percentage of "JK" & "ML" in B
right now Im doing through subsetting.
My method is something like:
df_B = df.loc[df['col_1']=='B']
df_B['col_2'].value_counts(normalize = True)*100
Is there any other method without doing the subset?
Thanks in advance
Upvotes: 0
Views: 48
Reputation: 323226
Check crosstab
pd.crosstab(df['col_1'],df['col_2'],normalize ='index')
Out[180]:
col_2 JK ML
col_1
A 1.000000 0.000000
B 0.333333 0.666667
Upvotes: 2