How to perform windowed operation on dataframe?

后端 未结 1 1537
自闭症患者
自闭症患者 2021-01-17 01:18

Given

import pandas as pd
import numpy as np

ssss = pd.DataFrame(np.arange(6))

ssss:

   0
0  0
1  1
2  2
3  3
4          


        
1条回答
  •  说谎
    说谎 (楼主)
    2021-01-17 02:02

    If windows are not overlapped you can use groupby.

    I think you need GroupBy.transform with integer division:

    #if default RangeIndex
    ssss['res'] = ssss.groupby(ssss.index // 2)[0].transform('mean')
    #any index - helper array
    ssss['res'] = ssss.groupby(np.arange(len(ssss)) // 2)[0].transform('mean')
    print (ssss)
       0  res
    0  0  0.5
    1  1  0.5
    2  2  2.5
    3  3  2.5
    4  4  4.5
    5  5  4.5
    

    EDIT:

    print (df)
       0  1  2
    0  0  5  a
    1  1  4  b
    2  2  3  c
    3  3  2  d
    4  4  1  e
    5  5  0  f
    
    def row_reduce(col0, col1):
        return str(2 * col0) + str(col1)
    
    def col_reduce(rows_data):
        return ",".join(rows_data)
    
    
    df['res'] = (df.apply(lambda x: row_reduce(x[0], x[1]), axis=1)
                   .groupby(df.index // 2)
                   .transform(col_reduce))
    print (df)
       0  1  2     res
    0  0  5  a   05,24
    1  1  4  b   05,24
    2  2  3  c   43,62
    3  3  2  d   43,62
    4  4  1  e  81,100
    5  5  0  f  81,100
    

    0 讨论(0)
提交回复
热议问题