How to add words from a DataFrame

0

I have a csv file with several columns of data, but I'm only interested in two, the nationality and the commune (region) where they live, obviously the two columns are not numerical data, something like this:

'index', 'comuna_name', 'country_name'

My idea is to count how many people of each nationality live in a certain commune, so far I am using this code

fa = (total.groupby('comuna_name')
                        .aggregate({'country_name': 'sum'})
                        .sort_values('country_name'))

The problem is that the exit does not deliver me in numbers, but "unites" the countries in this way

The question in essence is how to do in order to count the countries, to later graph. Thanks

    
asked by Rodrigo 31.07.2018 в 05:11
source

1 answer

0

This will surely help you.

con_paises = pd.get_dummies(total, columns=['country_name'], prefix='Pais')
fa = con_paises.groupby(by='comuna_name', as_index=False).sum()

What happens here is the following:

  • we convert each country into a different column with binary values with pd.get_dummies

  • We aggregate the information regarding 'comuna_name'

  • Your result is a table with a common pivot and counting the values for each country.

    PD .: On the other hand, if you want to count words without making a distinction of the country associated with 'comuna_name' then what you should do is count.

    fa = total.groupby(by='comuna_name', as_index=False).count()
    
        
    answered by 03.08.2018 в 20:47