1

I found an alternate answer here link

My dataframe currently looks like this

          date  new_cases   new_deaths  new_tests   year    month   day weekday
0   2019-12-31  0.0                 0.0       NaN   2019    12      31   1
1   2020-01-01  0.0                 0.0       NaN   2020    1        1   2
2   2020-01-02  0.0                 0.0       NaN   2020    1        2   3

I want to pass an code which will average out the 'new_cases' for weekday and weekend my code currently look like this but I can only pass 1 condition which is ' == 6 '. I want to pass multiple criteria, for example, == (4,5,6)

covid_df[covid_df.weekday == 6].new_cases.mean()

Any clue?

FastBoi
  • 163
  • 9

2 Answers2

1

I think you're looking for isin:

# you can use `loc` to access `new_cases` also
days = [4, 5, 6]
df.loc[df.weekday.isin(days), "new_cases"].mean()

which selects the rows whose weekday is in the days list; and then selects new_cases column.

Mustafa Aydın
  • 17,645
  • 4
  • 15
  • 38
1

You can groupby() date being a weekday or weekend. Following code sources and demonstrates this.

import requests
import io
import pandas as pd

dfall = pd.read_csv(io.StringIO(requests.get(
    "https://raw.githubusercontent.com/owid/covid-19-data/master/public/data/owid-covid-data.csv").text))
dfall["date"] = pd.to_datetime(dfall["date"])

dtlcols = ["date", "iso_code", "location", "continent"]
cols = ["new_cases", "new_deaths", "new_tests"]
dfall.loc[dfall["iso_code"].eq("GBR"), dtlcols + cols].groupby(
    dfall["date"].dt.dayofweek.isin([5, 6]), as_index=False
).agg({**{c: "last" for c in dtlcols}, **{c: "mean" for c in cols}})
date iso_code location continent new_cases new_deaths new_tests
0 2021-07-02 00:00:00 GBR United Kingdom Europe 9536.18 288.555 456697
1 2021-07-04 00:00:00 GBR United Kingdom Europe 9214.97 204.614 393841
Rob Raymond
  • 29,118
  • 3
  • 14
  • 30
  • Thank you for your answer, but I am too beginner to understand that. Thanks anyway for your answer! This code could solve my question as well, but I'm not sure how it works, hope somebody will answer that for me. – FastBoi Jul 05 '21 at 11:21
  • I'm a lazy typist :-). the only syntax sugar is building which columns we want in aggregate result with `agg()` – Rob Raymond Jul 05 '21 at 11:22