- 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.
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))
0
#something like this?
df['date'] = pd.to_datetime(df['date'], format= "%d-%m-%y").dt.datetime.strftime("%A")
0
Yes thats what ive been trying but i get errors
0
Does this help?
https://code.sololearn.com/cR57nxQLiCCn/?ref=app
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.
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.
0
Check the code again, it gives the day of week name
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))
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))
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))