Skip to content
Advertisement

Sorting entire csv by frequency of occurence in one column

I have a large CSV file, which is a log of caller data.

A short snippet of my file:

JavaScript

I want to sort the entire list by the frequency of occurrence of customers so it will be like:

JavaScript

I’ve tried groupby, but that only prints out the Company Name and the frequency but not the other columns, I also tried

JavaScript

and

JavaScript

But these give me errors:

ValueError: The wrong number of items passed 1, indices imply 24

I’ve looked at something like this:

JavaScript

but this only prints out two columns, and I want to sort my entire CSV. My output should be my entire CSV sorted by the first column.

Thanks for the help in advance!

Advertisement

Answer

This seems to do what you want, basically add a count column by performing a groupby and transform with value_counts and then you can sort on that column:

JavaScript

Output:

JavaScript

You can drop the extraneous column using df.drop:

JavaScript

Output:

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement