Home > front end >  How to create a bar plot of the number of unique values within each group
How to create a bar plot of the number of unique values within each group

Time:10-17

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

## the following is the data set

gm = pd.read_csv('https://raw.githubusercontent.com/gerberl/6G7V0026-2223/main/datasets/gapminder.tsv', sep='\t')

the command that I have been using, which counts each country multiple times.

sns.countplot(x=gm.continent)
plt.show 

I can get the plot graph by making a new data frame, however there must be a way to get the graph without making a new dataframe.

The bars would be for the total number of countries in each continent, where the x-axis will be continents.

CodePudding user response:

  • The most direct way is to use pandas to get the number of unique countries for each continent, and then plot directly with enter image description here

    pt DataFrame

               country
    continent         
    Africa          52
    Americas        25
    Asia            33
    Europe          30
    Oceania          2
    
  • Related