Selecting rows with a certain weekday in DataFrame in Python

后端 未结 2 1115
日久生厌
日久生厌 2021-01-11 20:00

I have a DataFrame with time index like:

df.index = [2013-09-09 06:23:18, 2013-09-10 07:09:05, ..., 2014-02-02 06:04:04]

How could I choose

相关标签:
2条回答
  • 2021-01-11 20:36

    You can get the weekday by df.index.weekday, note that Monday = 0 and Sunday = 6

    To select the rows on Monday, you can do

    df = df[df.index.weekday==0]
    
    0 讨论(0)
  • 2021-01-11 20:54

    Here "Date" is the column name. Creating new column "weekday"

    df["weekday"] = pd.to_datetime(df.Date).dt.dayofweek
    

    From this column you can select any weekday from dataframe

    df.loc[df["weekday"] ==6]
    

    Here 6 denotes "Saturday"

    0 讨论(0)
提交回复
热议问题