Problem in performing a Groupby of a Dataframe - Pandas

Asked

Viewed 421 times

-1

I’m trying to accomplish a groupy in a Dataframe that consists of cases of covid-19 per state in Brazil. However when I pass the function it returns me the following error in the figure below. There are cases of states that repeat themselves, so I would like to join, to have a better view of the data... inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

1 answer

1


Remove the argument axis=1 of your code.

Example:

import pandas as pd

>>> df = pd.DataFrame({
...     'cases': [2, 3, 4, 2, 4], 
...     'state': ['MG', 'MG', 'SC','RS', 'SC']
...     })

>>> df.groupby('state')
<pandas.core.groupby.generic.DataFrameGroupBy ...

>>> df.groupby('state').count()
       cases
state       
MG         2
RS         1
SC         2
  • Good morning mate, the code worked out above. Yesterday I tried this same way and unfortunately I was not going at all, even removing Axis=1. But when I perform this operation, instead of taking the data of the states that repeat themselves in the dataframe and adding, it simply returns inconsistent data, for example, in Pernambuco, there is no death and it returns to me that 24 people died...

  • In this code you posted, it only counts the times the state name repeats, and does not add up the occurrence cases in the states. MG = 5, RS = 4, SC = 8 ?

  • I didn’t know your purpose with the code, I was just showing that my example forms an object DataFrameGroupBy, as expected. To sum occurrences instead of counting them, use the method .sum() instead of .count(). More information on how to work with objects DataFrameGroupBy in this guide.

  • Worked replacing Count() for sum(). Thanks in advance. I will take a look at this guide that you have passed on!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.