I have a data frame which describes the height of 800 person, and I want to find the most frequent height of every 16 person. I know I can use this following script to find the most frequent value:
df['height'].value_counts().idxmax()
but it will only give me one the most frequent height among all. I've tried this following script to find the frequent height of each 16 rows:
grouper = df.groupby(df.index // 16)
df1 = grouper.agg(
df['height'].value_counts().idxmax()
)
but it gives me error that said the code can not find "height".
is there anyother way to find the most frequent value of each 16 rows in python?
Thankyou
You can do something like this (assuming your index is a range from 0 to n - else do reset_index()
twice):
df['groups'] = df.reset_index()['index'] // 16
df.groupby('groups').max()