简体   繁体   English

在Python中的DataFrame中选择具有特定工作日的行

[英]Selecting rows with a certain weekday in DataFrame in Python

I have a DataFrame with time index like:我有一个带有时间索引的 DataFrame,例如:

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

How could I choose rows in certain weekday, like Monday?我如何在某些工作日(例如星期一)选择行? Then I won't have the rows in other weekdays.然后我不会在其他工作日有这些行。 Any help appreciated.任何帮助表示赞赏。

You can get the weekday by df.index.weekday , note that Monday = 0 and Sunday = 6您可以通过df.index.weekday获取工作日,注意Monday = 0Sunday = 6

To select the rows on Monday, you can do要选择星期一的行,您可以执行

df = df[df.index.weekday==0]

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 "Sunday"这里的 6 表示“星期日”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM