- 1

Day of the Week python for data science challenge

How do I convert this code to have dashes in the formatting of the date? import pandas as pd df = pd.read_csv("/usercode/files/ca-covid.csv") df.drop('state', axis=1, inplace=True) df['date'] = pd.to_datetime(df['date'], format="%d.%m.%y")dt.datetime.strftime("%A") print(df.foot(7)) My output matches the answer except the date column has the wrong format. I’ve tried adjusting the dots to dashes in the 4th line, but I get an error.

20th Apr 2021, 7:36 PM
Fan Mason
10 Antworten
+ 2
Here is the solution: import pandas as pd df = pd.read_csv("/usercode/files/ca-covid.csv") df.drop('state', axis=1, inplace=True) df['date'] = pd.to_datetime(df['date'], format="%d.%m.%y") df['weekday']=(df['date']).dt.strftime("%A") print(df.tail(7))
9th Oct 2021, 5:17 PM
Owethu Sotomela
Owethu Sotomela - avatar
0
#something like this? df['date'] = pd.to_datetime(df['date'], format= "%d-%m-%y").dt.datetime.strftime("%A")
20th Apr 2021, 11:37 PM
Steven M
Steven M - avatar
0
Yes thats what ive been trying but i get errors
20th Apr 2021, 11:40 PM
Fan Mason
21st Apr 2021, 12:09 AM
Steven M
Steven M - avatar
0
As a general case, i think thats a great strategy that ill utilize in the future. However, i need help with this challenge in particular: You continue working with the COVID dataset for California. Now, add the weekday names for each row as a new column, named 'weekday'. Then, output the last 7 days data of the dataset. Do not set any index on the DataFrame.
21st Apr 2021, 12:13 AM
Fan Mason
0
More info from the problem: The given code converts the date column to datetime, so you do not need to change its format. Use the .dt.strftime("%A") function on the date column to convert it into a weekday name.
21st Apr 2021, 12:14 AM
Fan Mason
0
Check the code again, it gives the day of week name
21st Apr 2021, 12:17 AM
Steven M
Steven M - avatar
0
check this solution import pandas as pd df = pd.read_csv("/usercode/files/ca-covid.csv") df.drop('state', axis=1, inplace=True) df['date'] = pd.to_datetime(df['date'], format="%d.%m.%y") df['weekday']=(df['date']).dt.strftime("%A") print(df.tail(7))
4th May 2021, 5:39 PM
DHELEEPAN DT
DHELEEPAN DT - avatar
0
import pandas as pd df = pd.read_csv("https://www.sololearn.com/uploads/ca-covid.csv") df.drop('state', axis=1, inplace=True) df['date'] = pd.to_datetime(df['date'], format="%d.%m.%y").dt.strftime("%A") df['weekday']=(df['date']) print(df.tail(7))
28th Oct 2022, 1:37 PM
Aditya Khochare
Aditya Khochare - avatar
0
import pandas as pd df = pd.read_csv("/usercode/files/ca-covid.csv") df.drop('state', axis=1, inplace=True) df['date'] = pd.to_datetime(df['date'], format="%d.%m.%y") df['weekday'] = pd.to_datetime(df['date'], format="%d.%m.%y").dt.strftime("%A") print(df.tail(7))
17th Nov 2022, 11:38 PM
Benjamin Fadina
Benjamin Fadina - avatar