简体   繁体   中英

How To Loop Through Pandas df?

I have this df

Covid-19 意大利

and I want to create a loop that is capable to get specified value:

  • the sum of people (between M and F) vaccinated,
  • sum of first and sum of second dose
  • selected for name area

So with conditional string appears like this:

br = vaccini.loc[(vaccini['fornitore'] == 'Pfizer/BioNTech') & 
             (vaccini['fascia_anagrafica'] == '20-29') & 
             (vaccini['nome_area'] == 'Abruzzo')].sum()

有条件的结果

But I want to create a loop for more computational efficiency like:

for x in df if fornitore is (same word) if fascia_anagrafica is (same word) if nome_area is (same word) print(x)

I think what you want is a groupby.

cols = [s for s in vaccini.columns if s.startswith('sesso') or s.endswith('dose')]
vaccini.groupby(['fornitore', 'fascia_anagrafica', 'nome_area'])[cols].sum()

This would sum the provided columns within each group. If you want a specific sum, just query the resulting multi-index for the applicable row with the manufacturer, age, and location that's applicable.

Generally, with Pandas, you want to avoid doing loops if possible. There is usually some way to get around doing a loop if you look in the library, so there's a substantial element of research with Pandas (unless what you're looking for is pretty nonstandard).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM