How to deal with this complex logic in python pandas?

后端 未结 2 1425
没有蜡笔的小新
没有蜡笔的小新 2021-01-14 10:04

I have some data like follow structure. It used in python pandas Data Frame and I named it df.

Data1,Data2,Flag
2016-04-29,00:40:15,1
2016-04-29,00:40:24,2
2         


        
2条回答
  •  臣服心动
    2021-01-14 10:28

    I built a generator to produce the rows then used pd.concat

    def get_row(df):
        ref = None
        for i, row in df.iterrows():
            if ref is not None:
                cond1 = (row.Data2.total_seconds() - 
                         ref.Data2.total_seconds() > 18)
                cond2 = row.Flag != ref.Flag
            if ref is None or cond1 or cond2:
                yield row
                ref = row
    
    pd.concat([r for r in get_row(df)], axis=1).T
    


    Timing

    Because @Kartik insisted :-)

提交回复
热议问题