简体   繁体   中英

Pandas dataframe - How to sort (alphabetically) column values with value_counts

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:

在此处输入图像描述

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])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM