删除日期早于“今天”的行

时间:2018-05-16 21:40:03

标签: python pandas datetime dataframe

我一直在这里搜索和谷歌,并找到了许多示例,显示如何根据非移动日期变量删除旧日期,但无法弄清楚如何根据今天的日期删除数据行。在下面的示例中,我将如何删除比今天更早的日期(应删除一个值)然后保存它?真正的源文件将继续每天获取新数据,我将需要删除比“今天”更早的所有内容。

from datetime import datetime

import pandas as pd

data = {'date': ['2001-04-10 18:47:05.069722', '2018-05-16 18:47:05.119994', '2018-05-16 18:47:05.178768', '2018-05-16 18:47:05.230071', '2018-05-16 18:47:05.230071', '2018-05-16 18:47:05.280592', '2018-05-16 18:47:05.332662', '2018-05-16 18:47:05.385109', '2018-05-16 18:47:05.436523', '2018-05-16 18:47:05.486877'], 
        'battle_deaths': [34, 25, 26, 15, 15, 14, 26, 25, 62, 41]}

df = pd.DataFrame(data, columns = ['date', 'battle_deaths'])

df
                         date  battle_deaths
0  2001-04-10 18:47:05.069722             34
1  2018-05-16 18:47:05.119994             25
2  2018-05-16 18:47:05.178768             26
3  2018-05-16 18:47:05.230071             15
4  2018-05-16 18:47:05.230071             15
5  2018-05-16 18:47:05.280592             14
6  2018-05-16 18:47:05.332662             26
7  2018-05-16 18:47:05.385109             25
8  2018-05-16 18:47:05.436523             62
9  2018-05-16 18:47:05.486877             41

1 个答案:

答案 0 :(得分:5)

Pandas不惜一切代价让开发人员的生活更轻松。与to_datetime('today')比较并相应地过滤:

df[pd.to_datetime(df.date, errors='coerce') >= pd.to_datetime('today')]

                         date  battle_deaths
1  2018-05-16 18:47:05.119994             25
2  2018-05-16 18:47:05.178768             26
3  2018-05-16 18:47:05.230071             15
4  2018-05-16 18:47:05.230071             15
5  2018-05-16 18:47:05.280592             14
6  2018-05-16 18:47:05.332662             26
7  2018-05-16 18:47:05.385109             25
8  2018-05-16 18:47:05.436523             62
9  2018-05-16 18:47:05.486877             41

这将删除0 th 行。