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:

enter image description here

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:

enter image description here

try this:

south_count = df_south.groupby('tempbin_cons', sort=false ).size() 

looks though data sorted string.


Comments

Popular posts from this blog

amazon web services - S3 Pre-signed POST validate file type? -

c# - Check Keyboard Input Winforms -