PYTHON, Pandas Dataframe: how to select and read only certain rows

Viewed 310

For the purpose to be clear here is the code that works perfectly (of course I put only the beginning, the rest is not important here):

df = pd.read_csv(
'https://github.com/pcm-dpc/COVID-19/raw/master/dati-andamento-nazionale/'
'dpc-covid19-ita-andamento-nazionale.csv',
parse_dates=['data'], index_col='data') 
df.index = df.index.normalize()
ts = df[['nuovi_positivi']].dropna()
sts = ts.nuovi_positivi

So basically it takes some data from the online Github csv that you may find here: Link NAZIONALE and look at the "data" which is the italian for "date" and extract for every date the value nuovi_positivi and then it put it into the program.

Now I have to do the same thing with this json that you may find here Link Json

As you may see, now for every date there are 21 different values because Italy has 21 regions (Abruzzo Basilicata Campania and so on) but I am interested ONLY with the values of the region "Veneto", and I want to extract only the rows that contains "Veneto" under the label "denominazione_regione" to get for every day the value "nuovi_positivi".

I tried with:

df = pd.read_json('https://raw.githubusercontent.com/pcm-dpc/COVID-19/master/dati- 
json/dpc-covid19-ita-regioni.json' , parse_dates=['data'], index_col='data', 
index_row='Veneto') 

df.index = df.index.normalize()
ts = df[['nuovi_positivi']].dropna()
sts = ts.nuovi_positivi

but of course it doesn't work. How to solve the problem? Thanks

1 Answers

try this:

df = pd.read_json('https://raw.githubusercontent.com/pcm-dpc/COVID-19/master/dati-json/dpc-covid19-ita-regioni.json',
convert_dates =['data']) 
df.index = df['data']
df.index = df.index.normalize()
df = df[df["denominazione_regione"] == 'Veneto']
ts = df[['nuovi_positivi']].dropna()
sts = ts.nuovi_positivi
Related