Sort Pandas DataFrame by frequency of values in one column

FJJ

I have a DataFrame imported from a CSV file with a column "animal_name" that shows different animals like:

Dog
Dog
Cat
Fish
Dog
Cat

I am trying to sort the DataFrame in order of the frequency which all the animals appear, like:

Dog 
Dog
Dog
Cat
Cat
Fish

So far I have been able to find the total frequencies that each of these items occurs using:

animal_data.groupby(["animal_name"]).value_counts()

animal_species_counts = pd.Series(animal_data["animal_name"].value_counts())

However, I have been unable to sort them as intended. I have tried creating an additional column for frequency and using assign(), transform(), insert(), and sort.values() as suggested on other similar questions posted here like:

animal_data.assign(animal_species_counts=tree_data_copy.groupby("tree_type_name")[:"tree_type_name"].transform("tree_type_counts")).sort_values(by=["tree_type_counts","tree_type_name"],ascending=[True, False])

I tried variations of this, but either get errors that the data is unhashable, or that the DataFrame still looks exactly the same. Not sure where it's going wrong.

Bill the Lizard

Once you have the frequencies in a series (animal_species_counts) you can use that as a map to create a new column, then sort by that column.

import pandas as pd

animal_data = pd.DataFrame({'animal_name': ['Dog', 'Dog', 'Cat', 'Fish', 'Dog', 'Cat']})
print(animal_data)

animal_species_counts = animal_data["animal_name"].value_counts()
print(animal_species_counts)

animal_data["animal_species_counts"] = animal_data["animal_name"].map(animal_species_counts)
print(animal_data)

animal_data.sort_values(by='animal_species_counts', ascending=False, inplace=True)
print(animal_data)

This puts the whole dataframe in order by the counts column.

Final output:

  animal_name  animal_species_counts
0         Dog                      3
1         Dog                      3
4         Dog                      3
2         Cat                      2
5         Cat                      2
3        Fish                      1

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Sort by Frequency of Values in a Column - Pandas

From Dev

Frequency count of values in a column of a pandas DataFrame

From Java

Count frequency of values in pandas DataFrame column

From Dev

Count freq of one column values in pandas dataframe and tag each row with its frequency occurence number

From Dev

How to sort a pandas DataFrame on one column given an already ordered list of the values in that column?

From Dev

Sort dataframe by another on one column - pandas

From Java

how to sort pandas dataframe from one column

From Dev

Sort by one column, then group by another, in Pandas Dataframe?

From Dev

sort and count values in a column DataFrame (Python Pandas)

From Dev

Sort a Pandas dataframe by frequency and length

From Dev

Count frequency of values in pandas DataFrame

From Dev

Change values of one column in pandas dataframe

From Dev

pandas sort values of one column by the average of the other column

From Dev

Pandas - How to sort values in one column ascending and another column descending?

From Dev

Pandas Dataframe sort by a column

From Dev

dataframe.sort_values() buy multiple columns with key for one column

From Dev

Sort values in a dataframe by a column and take second one only if equal

From Dev

Pandas: filter one dataframe by multiple, simultaneous column values of another dataframe

From Dev

Pandas: How to sort dataframe rows by date of one column

From Dev

Sort only parts of pandas Dataframe based on column values

From Dev

Pandas Dataframe - sort a list of values in each row of a column

From Dev

Pandas Dataframe sort_values() multiple column with custom key

From Java

Pandas DataFrame bar plot with sort_values by other column

From Dev

Replace values from one column with another column Pandas DataFrame

From Dev

filtering a Pandas dataframe by one column and getting the sum of values in another column

From Dev

Pandas : Mapping one column values using other dataframe column

From Dev

Add values to one column of a pandas dataframe based on the values in another

From Dev

Pandas: Sort number of words in one column by the values of another

From Dev

Frequency of one specific value in a DataFrame column

Related Related

  1. 1

    Sort by Frequency of Values in a Column - Pandas

  2. 2

    Frequency count of values in a column of a pandas DataFrame

  3. 3

    Count frequency of values in pandas DataFrame column

  4. 4

    Count freq of one column values in pandas dataframe and tag each row with its frequency occurence number

  5. 5

    How to sort a pandas DataFrame on one column given an already ordered list of the values in that column?

  6. 6

    Sort dataframe by another on one column - pandas

  7. 7

    how to sort pandas dataframe from one column

  8. 8

    Sort by one column, then group by another, in Pandas Dataframe?

  9. 9

    sort and count values in a column DataFrame (Python Pandas)

  10. 10

    Sort a Pandas dataframe by frequency and length

  11. 11

    Count frequency of values in pandas DataFrame

  12. 12

    Change values of one column in pandas dataframe

  13. 13

    pandas sort values of one column by the average of the other column

  14. 14

    Pandas - How to sort values in one column ascending and another column descending?

  15. 15

    Pandas Dataframe sort by a column

  16. 16

    dataframe.sort_values() buy multiple columns with key for one column

  17. 17

    Sort values in a dataframe by a column and take second one only if equal

  18. 18

    Pandas: filter one dataframe by multiple, simultaneous column values of another dataframe

  19. 19

    Pandas: How to sort dataframe rows by date of one column

  20. 20

    Sort only parts of pandas Dataframe based on column values

  21. 21

    Pandas Dataframe - sort a list of values in each row of a column

  22. 22

    Pandas Dataframe sort_values() multiple column with custom key

  23. 23

    Pandas DataFrame bar plot with sort_values by other column

  24. 24

    Replace values from one column with another column Pandas DataFrame

  25. 25

    filtering a Pandas dataframe by one column and getting the sum of values in another column

  26. 26

    Pandas : Mapping one column values using other dataframe column

  27. 27

    Add values to one column of a pandas dataframe based on the values in another

  28. 28

    Pandas: Sort number of words in one column by the values of another

  29. 29

    Frequency of one specific value in a DataFrame column

HotTag

Archive