Skip to content Skip to sidebar Skip to footer

How Can I Delete Whole Day Rows On Condition Column Values.. Pandas

i have below times series data frames i wanna delete rows on condtion (check everyday) : check aaa>100 then delete all day rows (in belows, delete all 2015-12-01 rows because aa

Solution 1:

I think you need if MultiIndex first compare values of aaa by condition and then filter all values in first level by boolean indexing, last filter again by isin with inverted condition by ~:

print (df)
                      aaa
date       time          
2015-12-01 00:00:00     0
           00:15:00     0
           00:30:00     0
           00:45:00     0
2015-12-02 05:00:00     0
           05:15:00   200
           05:30:00     0
           05:45:00     0
2015-12-03 06:00:00     0
           06:15:00     0
           06:30:00  1000
           06:45:00  1000
           07:00:00  1000

lvl0 = df.index.get_level_values(0)
idx = lvl0[df['aaa'].gt(100)].unique()
print (idx)
Index(['2015-12-02', '2015-12-03'], dtype='object', name='date')

df = df[~lvl0.isin(idx)]
print (df)
                     aaa
date       time         
2015-12-01 00:00:00    0
           00:15:00    0
           00:30:00    0
           00:45:00    0

And if first column is not index only compare column date:

print(df)datetimeaaa02015-12-01  00:00:00012015-12-01  00:15:00022015-12-01  00:30:00032015-12-01  00:45:00042015-12-02  05:00:00052015-12-02  05:15:0020062015-12-02  05:30:00072015-12-02  05:45:00082015-12-03  06:00:00092015-12-03  06:15:000102015-12-03  06:30:001000112015-12-03  06:45:001000122015-12-03  07:00:001000idx=df.loc[df['aaa'].gt(100),'date'].unique()print(idx)
['2015-12-02''2015-12-03']

df=df[~df['date'].isin(idx)]print(df)datetimeaaa02015-12-01  00:00:00012015-12-01  00:15:00022015-12-01  00:30:00032015-12-01  00:45:000

Post a Comment for "How Can I Delete Whole Day Rows On Condition Column Values.. Pandas"