我有一些数据如跟随结构。它用在python pandas Data Frame中,我把它命名为df。
Data1,Data2,Value
2016-04-29,00:40:15,3
2016-04-29,00:40:24,2
2016-04-29,00:40:35,2
2016-04-29,00:40:36,2
2016-04-29,00:40:43,2
2016-04-29,00:40:45,2
2016-04-29,00:40:55,1
我想过滤以下条件的数据:
df['Value'] <= 2
df.row['Data2'] - df.former_row['Data2'] > 10
(former_row表示前一行。我不知道如何在python pandas中解决。)结果如下。
Data1,Data2,Value
2016-04-29,00:40:24,2
2016-04-29,00:40:35,2
2016-04-29,00:40:43,2
2016-04-29,00:40:55,1
如何在pandas中使用该功能来解决这个问题?
答案 0 :(得分:1)
只是为了确保我们使用相同的设置:
from StringIO import StringIO
import pandas as pd
text = """Data1,Data2,Value
2016-04-29,00:40:15,3
2016-04-29,00:40:24,2
2016-04-29,00:40:35,2
2016-04-29,00:40:36,2
2016-04-29,00:40:43,2
2016-04-29,00:40:45,2
2016-04-29,00:40:55,1"""
df = pd.read_csv(StringIO(text), sep=',')
df.Data1 = pd.to_datetime(df.Data1)
df.Data2 = pd.to_timedelta(df.Data2)
IIUC:
cond1 = df.Value <= 2
cond2 = df.Data2.dt.total_seconds().diff() > 10
df.loc[cond1 & cond2]