[英]Is there a way to transpose a column into rows with each row having the first value from the previous row deleted and a new tail value inserted?
Is there a way to transpose a column into rows with each row having the first value from the previous row deleted and a new tail value inserted such that the second value(value in second column in that row) from the previous row becomes first value(value in the first column in that row) in the next row.有没有办法将列转置为行,每行删除前一行的第一个值并插入一个新的尾值,使得前一行的第二个值(该行第二列中的值)成为第一个值(该行第一列中的值)在下一行中。 The resulting table has values in a sliding window fashion as they move from row to row until they are popped out of the table.结果表中的值以滑动 window 的方式从一行移动到另一行,直到它们从表中弹出。 I have used the Index function in Excel but it is tiresome as i have to update it for each new row and I have around 2000 rows to create.我在 Excel 中使用了索引 function 但它很烦人,因为我必须为每个新行更新它并且我有大约 2000 行要创建。 Thanks What I have done so far in the Excel =INDEX($A:$A,ROW(A1)*22-12+COLUMN(A1)) I have to update the number 22 for each new row up until 2000 and flash fill the rows manually.谢谢我到目前为止在 Excel =INDEX($A:$A,ROW(A1)*22-12+COLUMN(A1)) 中所做的工作我必须更新每个新行的数字 22 直到 2000 和 flash 填充行手动。
You want to do it in postgres, or eg pandas?您想在 postgres 中执行此操作,或者例如 pandas? In pandas, you could do sth like this:在 pandas 中,你可以这样做:
df = pd.DataFrame({'a':np.arange(10)}) # a is your original data-series
for shift in range(1,df.shape[0]):
df[shift] = df['a'].shift(-shift)
print(df)
a 1 2 3 4 5 6 7 8 9
0 0 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0
1 1 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 NaN
2 2 3.0 4.0 5.0 6.0 7.0 8.0 9.0 NaN NaN
3 3 4.0 5.0 6.0 7.0 8.0 9.0 NaN NaN NaN
4 4 5.0 6.0 7.0 8.0 9.0 NaN NaN NaN NaN
5 5 6.0 7.0 8.0 9.0 NaN NaN NaN NaN NaN
6 6 7.0 8.0 9.0 NaN NaN NaN NaN NaN NaN
....
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.