简体   繁体   English

通过比较时间戳删除 dataframe 行

[英]Drop dataframe rows by comparing timestamps

I have a dataframe that looks like the one below.我有一个 dataframe,如下图所示。 Since the dataframe has data from many different days, I want to keep only those with a specific date.由于 dataframe 有许多不同日期的数据,我只想保留那些具有特定日期的数据。 So I tried to check %d, %m for days and months respectively.所以我试着分别检查 %d, %m 几天和几个月。 I used the code follows but it doesn't drop the rows I wanted to.我使用了下面的代码,但它不会删除我想要的行。 Thanks in advance!提前致谢!

2018-08-29T00:03:09      12310                  
2018-08-23T00:08:10      21231              
2018-08-29T00:13:10      19.6                   
2018-08-29T00:18:10     19321                   
2018-08-30T00:23:10     182134                  
2018-02-29T00:28:10     172319                  
2018-09-19T00:33:10     1734                    
2018-08-29T00:38:10     1764                    
2018-08-29T00:43:10     169743                  
2018-11-17T00:48:10     16747                   
2018-08-29T00:53:10     17830

What I have so far到目前为止我所拥有的

temp = df['Timestamps'][0]
tempTS = datetime.datetime.strptime(str(df['Timestamps'][0]), "%Y-%m-%dT%H:%M:%S")    
firstDay = tempTS.strftime("%d")
firstMonth = tempTS.strftime("%m")


for i in df['Timestamps']:
    timestamp = datetime.datetime.strptime(str(i), "%Y-%m-%dT%H:%M:%S")        
    if ((timestamp.strftime("%d") != firstDay) and (timestamp.strftime("%m") != firstMonth):            
        df.drop(df.index[i])

Selecting dates by Boolean按 Boolean 选择日期

  • Use pandas.Series.dt .使用pandas.Series.dt
    • The .dt accessor has many methods, which makes it easy to select specific components of a datetime value. .dt访问器有很多方法,这使得 select 特定组件的日期时间值变得容易。
import pandas as pd

# create dataframe
data = {'DateTime': ['2018-08-29T00:03:09', '2018-08-23T00:08:10', '2018-08-29T00:13:10', '2018-08-29T00:18:10', '2018-08-30T00:23:10', '2018-02-28T00:28:10', '2018-09-19T00:33:10', '2018-08-29T00:38:10', '2018-08-29T00:43:10', '2018-11-17T00:48:10', '2018-08-29T00:53:10'],
        'Value': [12310.0, 21231.0, 19.6, 19321.0, 182134.0, 172319.0, 1734.0, 1764.0, 169743.0, 16747.0, 17830.0]}

df = pd.DataFrame(data)

# convert to datetime
df.DateTime = pd.to_datetime(df.DateTime, format='%Y-%m-%dT%H:%M:%S')

# conditions
first_day = df.DateTime.dt.day[0]
first_month = df.DateTime.dt.month[0]

# select rows not equal to conditions
df[(df.DateTime.dt.month != first_month) & (df.DateTime.dt.day != first_day)]

             DateTime     Value
5 2018-02-28 00:28:10  172319.0
6 2018-09-19 00:33:10    1734.0
9 2018-11-17 00:48:10   16747.0

# select rows equal to conditions
df[(df.DateTime.dt.month == first_month) & (df.DateTime.dt.day == first_day)].to_clipboard(sep='\\s+')

              DateTime     Value
0  2018-08-29 00:03:09   12310.0
2  2018-08-29 00:13:10      19.6
3  2018-08-29 00:18:10   19321.0
7  2018-08-29 00:38:10    1764.0
8  2018-08-29 00:43:10  169743.0
10 2018-08-29 00:53:10   17830.0
df.drop(df.index[i])

is not correct for dropping row.不适合删除行。 you're passing a date in string as i.您将字符串中的日期作为 i 传递。 you should write你应该写

 df.drop(df[df['Timestamps'] == i].index[0])

don't forget to set inplace = True if you want to completely remove row from datafame.如果要从 datafame 中完全删除行,请不要忘记设置 inplace = True。 so the complete line of code is:所以完整的代码行是:

df.drop(df[df['Timestamps'] == i].index[0],inplace=True)

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

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