简体   繁体   English

如何切片 Pandas 日期时间索引以始终选择第一天

[英]How to slice Pandas datetime index to always pick the first day

If I have a dataframe that looks like this:如果我有一个如下所示的数据框:

timestamp            lat        lon             sog                                     
2018-12-22 08:03:15  54.917200  13.358867      10.8                     
2018-12-22 08:04:05  54.916350  13.354700      10.8                     
2018-12-22 08:04:53  54.915533  13.350767      10.7  
2018-12-24 08:03:15  57.917200  9.358867       10.8                     
2018-12-24 08:04:05  57.916350  9.354700       10.8                     
2018-12-26 08:04:53  59.915533  5.350767       10.7                     

I would like to make a slice of the df to only work with the information from the first day.我想制作 df 的一部分,只处理第一天的信息。 If I know the date I can simply如果我知道日期,我可以简单地

first_day = df.loc['2018-12-22']

But I would like to automatically do this for a lot of .csv files and they all have different starting dates.但是我想为很多 .csv 文件自动执行此操作,并且它们都有不同的开始日期。 Is there a way to slice the Datetime index to get the first day only?有没有办法将日期时间索引切片以获得第一天? something like:就像是:

first_day = df.iloc[:day1] ?

and end up with:并最终得到:

timestamp            lat        lon             sog                                     
2018-12-22 08:03:15  54.917200  13.358867      10.8                     
2018-12-22 08:04:05  54.916350  13.354700      10.8                     
2018-12-22 08:04:53  54.915533  13.350767      10.7  

You can find the first day (min date) and use boolean indexing:您可以找到第一天(最小日期)并使用布尔索引:

for df in df_list:
    days = df.index.normalize()
    min_date = days.min()
    df[days == min_date]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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