Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selecting rows with a certain weekday in DataFrame in Python

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 rows in certain weekday, like Monday? Then I won't have the rows in other weekdays. Any help appreciated.

like image 287
user3271033 Avatar asked Feb 14 '14 08:02

user3271033


People also ask

How do I filter specific rows from a DataFrame?

You can use df[df["Courses"] == 'Spark'] to filter rows by a condition in pandas DataFrame. Not that this expression returns a new DataFrame with selected rows. You can also write the above statement with a variable.


1 Answers

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]
like image 178
waitingkuo Avatar answered Oct 20 '22 03:10

waitingkuo