我有一些看起来像这样的数据:
ID Value Starts Ends
0 A 1 2000-01-01 2000-06-01
1 A 2 2000-06-02 2000-12-31
2 A 1 2001-01-01 2001-06-01
3 A 1 2001-06-02 2001-12-31
我想要做的是折叠Id和value相同的连续行。理想情况下,输出将是:
ID Value Starts Ends
0 A 1 2000-01-01 2000-06-01
1 A 2 2000-06-02 2000-12-31
2 A 1 2001-01-01 2001-12-31
但是,如果您天真地采用np.min(Starts)
和np.max(Ends)
,则(A,1)会显示值(A,2)。
gb = df.groupby(['ID', 'Value'], as_index=False)
df = gb.agg({'Starts': np.min, 'Ends': np.max}, as_index=False)
ID Value Starts Ends
0 A 1 2000-01-01 2001-12-31
1 A 2 2000-06-02 2000-12-31
有没有一种有效的方法让Pandas做我想做的事?
答案 0 :(得分:1)
如果你添加一个列(让我们称之为"额外"),每次groupby类别改变时都会增加,你可以改为使用它。接下来的挑战是使新列的添加更有效,这是我能想到的使其更有效的矢量化方式。
increment = ((df.Value[:-1] != df.Value[1:]) | (df.ID[:-1] != df.ID[1:])).cumsum()
df["extra"] = pd.concat((pd.Series([0]),increment),ignore_index=True)
第一行采用显示不同行的布尔数组的累积和,然后第二行采用前面的零点并将其添加到数据帧。
然后你可以做
gb = df.groupby(['extra'], as_index=False)
df = gb.agg({'Starts': np.min, 'Ends': np.max}, as_index=False)
答案 1 :(得分:-1)
只需执行df.drop_duplicates(subset = ['ID','Value'],inplace = True) 这将删除您具有重复ID和值输入的行。