python - Pandas duplicate columns with a shift -
i have pandas
time series object 3 series , time axis. want train neural network time window , reason need create matrix of duplicated pandas columns different shifts. pandas.concat
manually, that'd take long time , won't flexible. i'm trying is:
# make 40 columns original dataframe first column , # make 20 columns original dataframe second column param_array = pandas.dataframe() in range(1, 41) : param_array = pandas.concat([param_array, input[[0]].shift(i * 2)], axis=1, ignore_index=true) in range(1, 21) : param_array= pandas.concat([param_array, input[[1]].shift(i * 2)], axis=1, ignore_index=true)
but appends time series @ end of first one, doesn't make new columns.
i suspect reference 'input[[0]]' not correct. example of trying own code below. new columns shifted values of original data:
import pandas pd d = {'one': pd.series(range(20)),'two':pd.series(range(20)[::-1])} df = pd.dataframe(d) print df param_array = pd.dataframe() in range(1, 5) : param_array = pd.concat([param_array, df['one'].shift(i * 2)], axis=1, ignore_index=true) in range(1, 5) : param_array = pd.concat([param_array, df['two'].shift(i * 2)], axis=1, ignore_index=true) print param_array
output:
1 2 0 0 19 1 1 18 2 2 17 3 3 16 4 4 15 5 5 14 6 6 13 7 7 12 8 8 11 9 9 10 10 10 9 11 11 8 12 12 7 13 13 6 14 14 5 15 15 4 16 16 3 17 17 2 18 18 1 19 19 0 0 1 2 3 4 5 6 7 0 nan nan nan nan nan nan nan nan 1 nan nan nan nan nan nan nan nan 2 0 nan nan nan 19 nan nan nan 3 1 nan nan nan 18 nan nan nan 4 2 0 nan nan 17 19 nan nan 5 3 1 nan nan 16 18 nan nan 6 4 2 0 nan 15 17 19 nan 7 5 3 1 nan 14 16 18 nan 8 6 4 2 0 13 15 17 19 9 7 5 3 1 12 14 16 18 10 8 6 4 2 11 13 15 17 11 9 7 5 3 10 12 14 16 12 10 8 6 4 9 11 13 15 13 11 9 7 5 8 10 12 14 14 12 10 8 6 7 9 11 13 15 13 11 9 7 6 8 10 12 16 14 12 10 8 5 7 9 11 17 15 13 11 9 4 6 8 10 18 16 14 12 10 3 5 7 9 19 17 15 13 11 2 4 6 8
Comments
Post a Comment