简体   繁体   中英

How to remove last 2 months records from 2 years records based on Date?

I have a DataFrame which having 2 years data, need to sort and remove recent 2 months records.

Ship_Date        Id

2019-10-29       i1 
2019-10-29       i2
2019-10-28       i3
2019-10-28       i4
....

I tried in this way but getting KeyError:

df.index = pd.DatetimeIndex(df.pop('Ship_Date'))
df.sort_index().last('2M').drop()

Any help please

Use DataFrame.drop with DatetimeIndex :

df.index = pd.DatetimeIndex(df.pop('Ship_Date'))
df = df.drop(df.sort_index().last('2M').index)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM