简体   繁体   中英

Create “buffer” matrix from dataframe with rolling window?

Given a dataframe of just one column, how can I convert it into another dataframe "buffer" (of size 2), described below:

df =

   0
0  1
1  2
2  3
3  4
4  4
5  5
6  5

expected_buffer =

   0 1
0  1 2
1  2 3
2  3 4 
3  4 5

This is my attempt:

def buff(df,past):
    arr1=df.values
    arr=arr1[0:past]
    for i in xrange(past,df.shape[0]-past+2):
        arr=np.append(arr,arr1[i:past+i],axis=0)
    return pd.DataFrame(arr)

Which returns the following:

   0
0  1
1  2
2  3
3  4
4  4
5  5
6  5

How to get the expected buff output ?

EDIT: By past I mean the buffer size. Using MATLAB notations: I have 5 element column vector

df = [1;2;3;4;5]

If past is 2, I should end up getting the following output:

buff = [1 2; 2 3; 3 4; 4 5]

If past is 3, then expected output should be

buff = [1 2 3; 2 3 4; 3 4 5]

If past is 4, then expected output is

buff = [1 2 3 4; 2 3 4 5]

So for n -element df and past=m , I would get a matrix of size (n-past+1) x past .

def buff(df, past):
    a = np.concatenate([df.values[i:i-past] for i in range(past)], axis=1)
    return pd.DataFrame(a, columns=list(range(past)))

buff(df, 2)

在此输入图像描述

buff(df, 3)

在此输入图像描述

buff(df, 4)

在此输入图像描述

buff(df, 5)

在此输入图像描述

import pandas as pd

def buff(s, n):
    return (pd.concat([s.shift(-i) for i in range(n)], axis=1)
              .dropna().astype(int))

s = pd.Series([1,2,3,4,5])
print(buff(s, 2))

#    0  0
# 0  1  2
# 1  2  3
# 2  3  4
# 3  4  5

print(buff(s, 3))

#    0  0  0
# 0  1  2  3
# 1  2  3  4
# 2  3  4  5

print(buff(s, 4))

#    0  0  0  0
# 0  1  2  3  4
# 1  2  3  4  5

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM