Home > Net >  Select data based on weekday and weekend pandas
Select data based on weekday and weekend pandas

Time:03-04

I have a df like this:

import pandas as pd
import numpy as np

datetime = [('2019-09-15 00:15:00.000000000'),
            ('2019-09-15 00:30:00.000000000'),
            ('2019-09-15 00:45:00.000000000'),
            ('2019-09-15 01:00:00.000000000'),
            ('2019-09-15 01:15:00.000000000'),
            ('2019-09-15 01:30:00.000000000'),
            ('2019-09-15 01:45:00.000000000'),
            ('2019-09-15 02:00:00.000000000'),
            ('2019-09-15 02:15:00.000000000')]
p =[494.76,486.36,484.68,500.64,482.16,483.84,483.0,478.8,493.08,474.6]
q = [47.88,33.6,41.16,0.0,0.0,0.0,0.0,0.0,0.0,0.0]

df = pd.DataFrame(zip(datetime, p, q), columns=['datetime','p','q'])
df['week'] (df['datetime'].astype('datetime64[ns]')   pd.Timedelta(seconds=-15*60)).dt.day_name()

And now that I have defined the days of the week where each day starts at 00:15:00 and ends at the 00:00:00 after 96 points. I want to separate them into two other df's. In one only the weekdays and in the other only the weekends. I've tried:

df_week = df[~df['datetime'].dt.day_name().isin(['Saturday','Sunday'])]
df_weekend = df[df['datetime'].dt.day_name().isin(['Saturday','Sunday'])] 

Although the code runs without errors, I'm having an output that includes one data for "Saturday" at the df_week. Like this:

enter image description here

Any idea how I can fix this?

CodePudding user response:

I am new-ish to Pandas but I think the following works:

df1 = df[df['week'].isin(['Sunday', 'Saturday'])]

df2 = df[~df['week'].isin(['Sunday', 'Saturday'])]

Hopefully someone more capable can also help to verify

CodePudding user response:

For solving this I use the .loc function:

monday = df.loc[(df['week'] == 'Monday')]
tuesday = df.loc[(df['week'] == 'Tuesday')]
df_w = pd.concat([monday,tuesday])

Not the fastest solution, but it worked

  • Related