So i have a dataframe:
dateRep day month year cases deaths country_name Land.area..sq..km.
0 2021-09-21 21 9 2021 1162 7 Austria 82520.0
1 2021-09-20 20 9 2021 1708 7 Austria 82520.0
2 2021-09-19 19 9 2021 2072 5 Austria 82520.0
3 2021-09-18 18 9 2021 2235 9 Austria 82520.0
4 2021-09-17 17 9 2021 2283 8 Austria 82520.0
... ... ... ... ... ... ... ... ...
6145 2021-03-05 5 3 2021 4069 15 Sweden 407310.0
6146 2021-03-04 4 3 2021 4882 19 Sweden 407310.0
6147 2021-03-03 3 3 2021 4873 18 Sweden 407310.0
6148 2021-03-02 2 3 2021 6191 23 Sweden 407310.0
6149 2021-03-01 1 3 2021 668975 13086 Sweden 407310.0
6150 rows × 8 columns
And my target is to make a new dataframe which has only rows that follow specific condition in ‘dateRep’ column
The condition is being a holiday (Sunday or Saturday) using .weekday() function
But the problem is when i tried doing
df.loc[(df['dateRep'].weekday() == 5) or (df['dateRep'].weekday == 6)]
I had an error AttributeError: 'Series' object has no attribute 'weekday'
What do i do? How do i sort out these rows and what is an easier way of doing it?
>Solution :
Convert the dateRep
to datetime Series and use .dt.
accessor:
df["dateRep"] = pd.to_datetime(df["dateRep"])
print(df.loc[(df["dateRep"].dt.weekday == 5) | (df["dateRep"].dt.weekday == 6)])
Prints:
dateRep day month year cases deaths country_name Land.area..sq..km.
2 2021-09-19 19 9 2021 2072 5 Austria 82520.0
3 2021-09-18 18 9 2021 2235 9 Austria 82520.0
OR: Use .isin()
for shorter code:
df.loc[df["dateRep"].dt.weekday.isin([5, 6])]