How to use the Timestamp(). dayofweek method on a dataframe?

Asked

Viewed 63 times

0

I started to study python a short time ago and a problem has arisen that I am not able to solve. I have a csv file containing two columns('ds' = dates and 'y'= data of any result of the respective date), in this file I need to group all dates by days of the week to be able to do my analysis in the future. But I can’t figure out a way to manipulate the data frame in a way that can accomplish this. Here’s the test code I tried to do.

The csv file I’m trying to manipulate: https://github.com/facebook/prophet/blob/master/examples/example_wp_log_peyton_manning.csv

If anyone can help me I really appreciate.

from datetime import date
import pandas as pd
df = pd.read_csv('/content/drive/My Drive/example.csv',delimiter=',') #Lê o arquivo
df[pd.Timestamp(df['ds']).dayofweek]

1 answer

1

To set the day of the week you can use:

pd.to_datetime(df['ds']).dt.dayofweek

If you want to group all dates into one list per day you can use :

df.groupby(pd.to_datetime(df['ds']).dt.dayofweek).ds.apply(list)

If it is the sum of y:

df.groupby(pd.to_datetime(df['ds']).dt.dayofweek).y.sum()

Browser other questions tagged

You are not signed in. Login or sign up in order to post.