hernanavella
hernanavella

Reputation: 5552

How to slice pandas dataframe based on attribute of column name?

I have a dataframe where the column names are datatime objects like this:

            17:02:00    17:03:00 ...    
2017-09-18  1236.0      463.0   
2017-09-19  791.0       476.0   
2017-09-20  2086.0      988.0   

How can I select a slice of the dataframe based on the column name values like this pseudo code:

df_slice = df.column_name >= datetime.time(8, 30) & df.column_name < datetime.time(9, 0)

Upvotes: 1

Views: 442

Answers (2)

jezrael
jezrael

Reputation: 862406

I think you need first convert values to times by to_datetime + time and then select columns by condition:

df.columns = pd.to_datetime(df.columns).time

df_slice = df.loc[:, (df.columns >= datetime.time(8, 30)) & 
                     (df.columns < datetime.time(9, 0))]

Sample:

print (df)
            17:02:00  08:45:00
2017-09-18    1236.0     463.0
2017-09-19     791.0     476.0
2017-09-20    2086.0     988.0

df.columns = pd.to_datetime(df.columns).time

df_slice = df.loc[:, (df.columns >= datetime.time(8, 30)) & 
                     (df.columns < datetime.time(9, 0))]

print (df_slice)
            08:45:00
2017-09-18     463.0
2017-09-19     476.0
2017-09-20     988.0

Thank you @Zero for simplify answer:

df.loc[:, (df.columns > '08:30:00') & (df.columns < '09:00:00')]

Upvotes: 4

sparc_spread
sparc_spread

Reputation: 10833

I would say something like:

from datetime import datetime 

to_dt = lambda x : datetime.strptime(x, '%H:%M:%S')

time_1 = to_dt('08:30:00')
time_2 = to_dt('09:00:00')

cols_to_use = [
    i for i in df.columns if 
        to_dt(i) >= time_1 and 
        to_dt(i) <= time_2
]

df_slice = df.loc[:, cols_to_use]

So in other words all the comparison is happening outside of pandas. I'm sure this could be done as a one-liner but I've spaced it out a bit to make it easier to read.

Upvotes: 1

Related Questions