Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Display multiple values in col on barplot

So I have a column of my dataframe that looks like this:

10 first rows of the column expeditions[‘first_ascent_country’]

0    New Zealand, USA, UK
1                      UK
2                  France
3               UK, Nepal
4                   India
5               W Germany
6                   Spain
7               W Germany
8            Japan, Nepal
9            Japan, Nepal

It gives information on which country climbed a mountain first. I want to display those countries in a barplot to show which ones discovered more, but I get this… How do I separate the countries using a comma?
enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

How can I make it so that the rows with multiple countries are displayed on the chart separately? I have been looking everywhere but idk how to make it work…
Sorry if my question is stupid but I’m pretty new to working with dataframes.

>Solution :

Try:

df['peak_ascent_country'].str.split(', ').explode().value_counts().plot.barh()
plt.tight_layout()
plt.show()

enter image description here

Step by step:

>>> df['peak_ascent_country'].str.split(', ')
0    [New Zealand, USA, UK]
1                      [UK]
2                  [France]
3               [UK, Nepal]
4                   [India]
5               [W Germany]
6                   [Spain]
7               [W Germany]
8            [Japan, Nepal]
9            [Japan, Nepal]
Name: peak_ascent_country, dtype: object
>>> df['peak_ascent_country'].str.split(', ').explode().value_counts()
0    New Zealand
0            USA
0             UK
1             UK
2         France
3             UK
3          Nepal
4          India
5      W Germany
6          Spain
7      W Germany
8          Japan
8          Nepal
9          Japan
9          Nepal
Name: peak_ascent_country, dtype: object
>>> df['peak_ascent_country'].str.split(', ').explode().value_counts()
UK             3
Nepal          3
W Germany      2
Japan          2
New Zealand    1
USA            1
France         1
India          1
Spain          1
Name: peak_ascent_country, dtype: int64
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading