如何从大型数据框中删除特定日期时间戳

问题描述 投票:0回答:2

我有一个包含600天数据的大数据框。每天有100个时间戳。我有一个30天的单独列表,我想从中获取数据。如何从数据框中删除这30天内的数据?我尝试了一个for循环,但它没有用。我知道有一个简单的方法。但我不知道如何实现它。

df #is main dataframe which has many columns and rows. Index is a timestamp. 

df['dates'] = df.index.strftime('%Y-%m-%d') # date part of timestamp is sliced and  
#a new column is created. Instead of index, I want to use this column for comparing with bad list. 
bad_list # it is a list of bad dates   
for i in range(0,len(df)):
    for j in range(0,len(bad_list)):
        if str(df['dates'][i])== bad_list[j]:
            df.drop(df[i].index,inplace=True)
python pandas dataframe
2个回答
0
投票

您可以执行以下操作

df['dates'] = df.index.strftime('%Y-%m-%d') 
#badlist should be in date format too. 

newdf = df[~df['dates'].isin(badlist)]
# the ~ is used to denote "not in" the list.

#if Jan 1, 2000 is a bad date, it should be in the list as datetime(2000,1,1)

0
投票

你可以进行简单的比较:

>>> dates = pd.Series(pd.to_datetime(np.random.randint(int(time()) - 60 * 60 * 24 * 5, int(time()), 12), unit='s'))
>>> dates
0    2019-03-19 05:25:32
1    2019-03-20 00:58:29
2    2019-03-19 01:03:36
3    2019-03-22 11:45:24
4    2019-03-19 08:14:29
5    2019-03-21 10:17:13
6    2019-03-18 09:09:15
7    2019-03-20 00:14:16
8    2019-03-21 19:47:02
9    2019-03-23 06:19:35
10   2019-03-23 05:42:34
11   2019-03-21 11:37:46

>>> start_date = pd.to_datetime('2019-03-20')
>>> end_date = pd.to_datetime('2019-03-22')
>>> dates[(dates > start_date) & (dates < end_date)]
1    2019-03-20 00:58:29
5    2019-03-21 10:17:13
7    2019-03-20 00:14:16
8    2019-03-21 19:47:02
11   2019-03-21 11:37:46

如果您的源Series不是datetime格式,那么您将需要使用pd.to_datetime进行转换。

© www.soinside.com 2019 - 2024. All rights reserved.