1

I have a pandas dataframe as following below:

import pandas as pd

df = pd.DataFrame({'DAY_OF_WEEK': ['Monday', 'Sunday', 'Saturday', 'Monday', 'Tuesday'],
                   'HOUR': ['21:02:02', '11:22:02', '23:02:02', '09:22:02', '09:02:02'],
              'id': [1,2,2,3,1]})

df

I need to applicate a filter for that a new dataframe have only register where HOUR between 19:00:00 and 07:00:00 or DAY_OF_WEEK == 'Sunday'. An expected output:

DAY_OF_WEEK |   HOUR  | ID
Monday      |21:02:02 | 1
Saturday    |23:02:02 | 2
Monday      |09:22:02 | 3

I trying the following:

df2 = df.loc[(pd.to_timedelta(df.HOUR).between('06:00:00','19:00:00') | df['DAY_OF_WEEK'] == 'Sunday')]
calls_night_or_sunday = df[~df2].copy()

but, I have an output where all the lines received NaN. Can anyone help?

Costa.Gustavo
  • 849
  • 10
  • 21

4 Answers4

2
import pandas as pd

df = pd.DataFrame({'DAY_OF_WEEK': ['Monday', 'Sunday', 'Saturday', 'Monday', 'Tuesday'],
                   'HOUR': ['21:02:02', '11:22:02', '23:02:02', '09:22:02', '09:02:02'],
              'id': [1,2,2,3,1]})

df2 = df[(df.HOUR.between('19:00:00','23:59:59')) | (df.HOUR.between('00:00:00','06:00:00')) | (df.DAY_OF_WEEK == 'Sunday')]
df2

# Output
# DAY_OF_WEEK   HOUR    id
# 0 Monday  21:02:02    1
# 1 Sunday  11:22:02    2
# 2 Saturday    23:02:02    2

Pandas conditions

UPDATED:
timeranges changed

1

You can use:

df.loc[('19' < df['HOUR']) | (df['HOUR'] < '06') |  (df['DAY_OF_WEEK'] == 'Sunday')]

Output:

  DAY_OF_WEEK      HOUR  id
0      Monday  21:02:02   1
1      Sunday  11:22:02   2
2    Saturday  23:02:02   2
Mykola Zotko
  • 15,583
  • 3
  • 71
  • 73
-1

Try using "|" instead of "or"

ahmed awada
  • 115
  • 5
-1

Try this instead

df2 = df.loc[(pd.to_timedelta(df.HOUR).between('06:00:00','19:00:00') | (df['DAY_OF_WEEK'] == 'Sunday'))]
Dharman
  • 30,962
  • 25
  • 85
  • 135
Paul Brennan
  • 2,638
  • 4
  • 19
  • 26