Home > Mobile >  How to filter data from a dataframe so that only values ​linked to a specific string remain. Python
How to filter data from a dataframe so that only values ​linked to a specific string remain. Python

Time:11-27

so I got this dataframe showing the leading causes of death for each year in Chile.

Original Dataframe

What I want to do is to make something like this:

What i want to make

I want to make that so I can see how that specific cause of death varies in the years shown. I made the dataframe so "Causas 2 de año 2016" is a different column to "% 1" ( 16) Later I want to try plotting these variations.

I'm new on using Python, right now im using it on Jupyter Notebook. Thanks in advance

I tried using .loc but i absolutely failed. Really dont know how to aproach the problem

CodePudding user response:

You should use df.query. With that function one can make a filter from the dataframe

CodePudding user response:

Probably is there a better way, but in the meantime this give exactly what you asked for

import pandas as pd

df = pd.read_csv("causas.csv")

causa="Enfermedades cerebrovasculares"

i = df.columns
df3=pd.DataFrame()
for j in range (0,14,2):
    df2=df[df[i[j]]==causa][[i[j],i[j 1]]]
    df3[i[j]]=df2[i[j]].to_list()
    df3[i[j 1]]=df2[i[j 1]].to_list()

print(df3)
  • Related