合并大熊猫中的时间段数据

时间:2017-03-01 05:16:18

标签: python pandas

如何在Python pandas中整合时间段数据?

我想操纵

中的数据
person  start       end
1       2001-1-8   2002-2-14
1       2002-2-14  2003-3-1
2       2001-1-5   2002-2-16
2       2002-2-17  2003-3-9

person  start       end
1       2001-1-8   2002-3-1
2       2001-1-5   2002-3-9

我想先检查最后end和新start是否在1天之内。如果没有,则保留原始数据结构,如果是,则合并。

2 个答案:

答案 0 :(得分:0)

df.sort_values(["person", "start", "end"], inplace=True)

def condense(df):
    df['prev_end'] = df["end"].shift(1)
    df['dont_condense'] = (abs(df['prev_end'] - df['start']) > timedelta(days=1))
    df["group"] = df['dont_condense'].fillna(False).cumsum()
    return df.groupby("group").apply(lambda x: pd.Series({"person": x.iloc[0].person, 
                                               "start": x.iloc[0].start, 
                                               "end": x.iloc[-1].end}))

df.groupby("person").apply(condense).reset_index(drop=True)

答案 1 :(得分:0)

如果每个组仅包含2行并且需要差异10天,您也可以使用,所有数据都会排序:

print (df)
   person      start        end
0       1   2001-1-8  2002-2-14
1       1  2002-2-14   2003-3-1
2       2   2001-1-5  2002-2-16
3       2  2002-2-17   2003-3-9
4       3   2001-1-2  2002-2-14
5       3  2002-2-17  2003-3-10

df.start = pd.to_datetime(df.start)
df.end = pd.to_datetime(df.end)

def f(x):
    #if need difference only 0 days, use
    #a = (x['start'] - x['end'].shift()) == pd.Timedelta(days=0) 
    a = (x['start'] - x['end'].shift()).isin([pd.Timedelta(days=1), pd.Timedelta(days=0)])
    if a.any():
        x.end = x['end'].shift(-1)
    return (x)

df1 = df.groupby('person').apply(f).dropna().reset_index(drop=True)
print (df1)
   person      start        end
0       1 2001-01-08 2003-03-01
1       2 2001-01-05 2003-03-09
2       3 2001-01-02 2002-02-14
3       3 2002-02-17 2003-03-10