python - Preserving order of occurrence with size() function -
i preserve order of dataframe when using .size() function. first dataframe created choosing subset of larger one:
df_south = df[df['region_name'] == 'south']
here example of dataframe looks like:
with dataframe count occurrences of each unique 'tempbin_cons' variable.
south_count = df_south.groupby('tempbin_cons').size()
i maintain order exists using sort column. created column based on order 'tempbin_cons' variable appear after counting. can't seem appear in proper order though. i've tried using .sort_index() on south_count , not change order groupby() creates.
ultimately solution fixing axis ordering of bar plot creating of south_count. ordering difficult read , appear in logical order.
for reference south_count, , subsequently axis of bar plot appears in order:
try this:
south_count = df_south.groupby('tempbin_cons', sort=false ).size()
looks though data sorted string.
Comments
Post a Comment