Let's say I create the following data file with df.set_index ('Class', 'subclass'), bearing in mind that there are several classes with subclasses ... A> Z.
Class subclass
A a
A b
A c
A d
B a
B b
How do I count subclasses in a class and create a separate column named no classes so that I can see the class with the most subclasses? I was thinking of some kind of loop that runs through the letters of the class and counts the subclass if this class letter is anyway. However, for such a problem, this seems a bit controversial. Would there be a simpler approach, for example, df.groupby [] count?
Desired Result:
Class subclass No. of classes
A a 4
A b
A c
A d
B a 2
B b
I tried the level parameter as shown in the pandas dataframe group multi-index , but for me it does not work
EDIT:
, . :
df.reset_index().groupby('Class')['subclass'].nunique().idxmax()