Separate a Dataframe

Asked

Viewed 33 times

0

I would like to know a practical way of separating a Dataframe into several by the column value state.

For example:

AC = covid[covid['state'] == 'AC'],   
AL = covid[covid['state'] == 'AL'],   
AM = covid[covid['state'] == 'AM'],   
AP = covid[covid['state'] == 'AP'],   

I did it this way, but it becomes very repetitive. I tried in other ways, but I ended up not getting.

inserir a descrição da imagem aqui

  • Does it have to be done this way? What is your intention to create several variables? Hug!

  • It may be otherwise but what I wanted was to calculate the percentage variation of cases and deaths of covid for each state, for that I needed to separate the states calculate the variation and then put it all together again.

  • can share the database?

  • https://brasil.io/dataset/covid19/files/

1 answer

0

Maybe the groupby and the pct_change pandas help you

df['pct_change_new_confirmed'] = df.groupby('state')['new_confirmed'].pct_change().fillna(0)

df['pct_change_new_deaths'] = df.groupby('state')['new_deaths'].pct_change().fillna(0)
  • groupby used to make the grouping
  • pct_change calculates the percentage change
  • Thanks for the tip I forgot the abs groupby!

Browser other questions tagged

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