简体   繁体   English

有没有办法将列转置为行,每行删除前一行的第一个值并插入一个新的尾值?

[英]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.

相关问题 有没有办法根据现有列的当前和前一行值在每行的新列中分配增量值的数量? - Is there a way to assign number of incremental value in a new column of each rows based current and previous row values of existing columns? 添加新列,每行作为另一列的前一个组值 - Add new column with each row as previous group value from another column 使用pySpark中第二列的值对行进行转置 - Row transpose with value from a second column in pySpark 返回新列中每一行中第一个匹配值的列名 - Return column name of the first matching value in each row in a new column Spark使用上一行的值将新列添加到数据框 - Spark add new column to dataframe with value from previous row 在 Python 中创建新列并使用上一行的值 - Create new column in Python and use value from previous row 有没有办法为一行中的每个值分配一个日期,然后转置df? - Is there a way to assign a date to each value in a row, and then transpose the df? Select 行按列值并按另一个列值包含前一行 - Select rows by column value and include previous row by another column value 有没有办法用列名、第一列中的行值和值本身替换数据框中的每个单元格值? - Is there a way to replace each cell value in a dataframe with the column name, row value in the first column and the value itself? 选择当前行中列值为 1 但上一行中值为 0 的行 - Selecting rows where column value is 1 in the current row, but 0 in the previous row
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM