Reputation: 37
I am trying to sort dataframe column values in conjunction with value_count -
Below is a code snippet of my algorithm:
with open (f_out_txt_2, 'w', encoding='utf-8') as f_txt_out_2:
f_txt_out_2.write(f"SORTED First Names w/SORTED value counts:\n")
for val, cnt in df['First Name'].value_counts(sort='True').iteritems():
f_txt_out_2.write("\n{0:9s} {1:2d}".format(val, cnt))
Below is the first few lines of output - note that "First Name" values are not in alphabetic order. How can I get the "First Name" values sorted while keeping value counts sorted?
Output: SORTED First Names w/SORTED value counts: Marilyn 11 Todd 10 Jeremy 10 Barbara 10 Sarah 9 Rose 9 Kathy 9 Steven 9 Irene 9 Cynthia 9 Carl 8 Alice 8 Justin 8 Bobby 8 Ruby 8 Gloria 8 Julie 8 Clarence 8 Harry 8 Andrea 8
.... Unfortunately I can't find the original source link of where I downloaded the "employee.csv" file from, but here is a sample of it to give an idea of what it contained:
Upvotes: 0
Views: 1348
Reputation: 8768
I believe you would use the following code to sort by first name, then by value counts.
dfg = df.groupby('First Name').agg(value_count = ('First Name','count')).sort_values(by = ['First Name','value_count'], ascending = [True,False])
Upvotes: 0