How to find most frequent data of a catagorical data in pandas?
Answers
Answered by
0
Often while working with pandas dataframe you might have a column with categorical variables, string/characters, and you want to find the frequency counts of each unique elements present in the column. Pandas’ value_counts() easily let you get the frequency counts.
Let us get started with an example from a real world data set.
Load gapminder data set
# import pandas as pd import pandas as pd # software carpentry url for gapminder data gapminder_csv_url ='http://bit.ly/2cLzoxH' # load the data with pd.read_csv gapminder = pd.read_csv(gapminder_csv_url)
Frequency Counts of the column “continent”
Let us say we want to find the frequency counts of column ‘continent’ in the data frame. We can use pandas’ function value_counts on the column of interest. It will return NumPy array with unique items and the frequency of it.
>gapminder['continent'].value_counts() Africa 624 Asia 396 Europe 360 Americas 300 Oceania 24
If you just want the unique values from a pandas dataframe column
Let us get started with an example from a real world data set.
Load gapminder data set
# import pandas as pd import pandas as pd # software carpentry url for gapminder data gapminder_csv_url ='http://bit.ly/2cLzoxH' # load the data with pd.read_csv gapminder = pd.read_csv(gapminder_csv_url)
Frequency Counts of the column “continent”
Let us say we want to find the frequency counts of column ‘continent’ in the data frame. We can use pandas’ function value_counts on the column of interest. It will return NumPy array with unique items and the frequency of it.
>gapminder['continent'].value_counts() Africa 624 Asia 396 Europe 360 Americas 300 Oceania 24
If you just want the unique values from a pandas dataframe column
Similar questions