简体   繁体   English

"如何在 Pandas DataFrame 中移动一列"

[英]How to shift a column in Pandas DataFrame

I would like to shift a column in a Pandas DataFrame<\/code> , but I haven't been able to find a method to do it from the documentation without rewriting the whole DF.我想在 Pandas DataFrame<\/code>中移动一列,但我无法在不重写整个 DF 的情况下从文档中找到一种方法。 Does anyone know how to do it?有谁知道该怎么做? DataFrame:数据框:

##    x1   x2
##0  206  214
##1  226  234
##2  245  253
##3  265  272
##4  283  291
In [18]: a
Out[18]: 
   x1  x2
0   0   5
1   1   6
2   2   7
3   3   8
4   4   9

In [19]: a['x2'] = a.x2.shift(1)

In [20]: a
Out[20]: 
   x1  x2
0   0 NaN
1   1   5
2   2   6
3   3   7
4   4   8

You need to use df.shift here.你需要在这里使用df.shift
df.shift(i) shifts the entire dataframe by i units down. df.shift(i)将整个数据帧向下移动i单位。

So, for i = 1 :所以,对于i = 1

Input:输入:

    x1   x2  
0  206  214  
1  226  234  
2  245  253  
3  265  272    
4  283  291

Output:输出:

    x1   x2
0  Nan  Nan   
1  206  214  
2  226  234  
3  245  253  
4  265  272 

So, run this script to get the expected output:因此,运行此脚本以获得预期的输出:

import pandas as pd

df = pd.DataFrame({'x1': ['206', '226', '245',' 265', '283'],
                   'x2': ['214', '234', '253', '272', '291']})

print(df)
df['x2'] = df['x2'].shift(1)
print(df)

Lets define the dataframe from your example by让我们从您的示例中定义数据框

>>> df = pd.DataFrame([[206, 214], [226, 234], [245, 253], [265, 272], [283, 291]], 
    columns=[1, 2])
>>> df
     1    2
0  206  214
1  226  234
2  245  253
3  265  272
4  283  291

Then you could manipulate the index of the second column by然后你可以操作第二列的索引

>>> df[2].index = df[2].index+1

and finally re-combine the single columns最后重新组合单列

>>> pd.concat([df[1], df[2]], axis=1)
       1      2
0  206.0    NaN
1  226.0  214.0
2  245.0  234.0
3  265.0  253.0
4  283.0  272.0
5    NaN  291.0

Perhaps not fast but simple to read.也许不快但很容易阅读。 Consider setting variables for the column names and the actual shift required.考虑为列名和所需的实际移位设置变量。

Edit: Generally shifting is possible by df[2].shift(1) as already posted however would that cut-off the carryover.编辑:通常可以通过df[2].shift(1)转移,正如已经发布的那样,但是这会切断结转。

If you don't want to lose the columns you shift past the end of your dataframe, simply append the required number first:如果您不想丢失移过数据框末尾的列,只需先附加所需的数字:

    offset = 5
    DF = DF.append([np.nan for x in range(offset)])
    DF = DF.shift(periods=offset)
    DF = DF.reset_index() #Only works if sequential index

I suppose imports我想进口

import pandas as pd
import numpy as np

First append new row with NaN, NaN,... at the end of DataFrame ( df ).首先在 DataFrame ( df ) 的末尾附加带有NaN, NaN,...新行。

s1 = df.iloc[0]    # copy 1st row to a new Series s1
s1[:] = np.NaN     # set all values to NaN
df2 = df.append(s1, ignore_index=True)  # add s1 to the end of df

It will create new DF df2.它将创建新的 DF df2。 Maybe there is more elegant way but this works.也许有更优雅的方式,但这是有效的。

Now you can shift it:现在你可以改变它:

df2.x2 = df2.x2.shift(1)  # shift what you want

Trying to answer a personal problem and similar to yours I found on Pandas Doc what I think would answer this question:试图回答一个个人问题,类似于你的问题,我在Pandas Doc上找到了我认为可以回答这个问题的内容:

DataFrame.shift (periods=1, freq=None, axis=0) Shift index by desired number of periods with an optional time freq DataFrame.shift (periods=1, freq=None,axis=0) 使用可选的时间频率按所需的周期数移动索引

Notes笔记

If freq is specified then the index values are shifted but the data is not realigned.如果指定了 freq,则索引值会移动,但不会重新对齐数据。 That is, use freq if you would like to extend the index when shifting and preserve the original data.也就是说,如果您想在移动时扩展索引并保留原始数据,请使用 freq。

Hope to help future questions in this matter.希望对以后的问题有所帮助。

df3

    1   108.210 108.231
2   108.231 108.156
3   108.156 108.196
4   108.196 108.074
... ... ...
2495    108.351 108.279
2496    108.279 108.669
2497    108.669 108.687
2498    108.687 108.915
2499    108.915 108.852

df3['yo'] = df3['yo'].shift(-1)

    yo  price
0   108.231 108.210
1   108.156 108.231
2   108.196 108.156
3   108.074 108.196
4   108.104 108.074
... ... ...
2495    108.669 108.279
2496    108.687 108.669
2497    108.915 108.687
2498    108.852 108.915
2499    NaN 108.852

This is how I do it:这就是我的做法:

df_ext = pd.DataFrame(index=pd.date_range(df.index[-1], periods=8, closed='right'))
df2 = pd.concat([df, df_ext], axis=0, sort=True)
df2["forecast"] = df2["some column"].shift(7)

Basically I am generating an empty dataframe with the desired index and then just concatenate them together.基本上我正在生成一个带有所需索引的空数据框,然后将它们连接在一起。 But I would really like to see this as a standard feature in pandas so I have proposed an enhancement to pandas.但我真的很想将其视为熊猫的标准功能,因此我提出了对熊猫的增强

I'm new to pandas, and I may not be understanding the question, but this solution worked for my problem:我是熊猫新手,我可能不理解这个问题,但这个解决方案适用于我的问题:

# Shift contents of column 'x2' down 1 row
df['x2'] = df['x2'].shift()

Or, to create a new column with contents of 'x2' shifted down 1 row或者,创建一个内容为“x2”的新列向下移动 1 行

# Create new column with contents of 'x2'  shifted down 1 row
df['x3'] = df['x2'].shift()

I had a read of the official docs for shift() while trying to figure this out, but it doesn't make much sense to me, and has no examples referencing this specific behavior.在试图弄清楚这一点时,我阅读了 shift() 的官方文档,但这对我来说没有多大意义,并且没有引用此特定行为的示例。

Note that the last row of column 'x2' is effectively pushed off the end of the Dataframe.请注意,“x2”列的最后一行被有效地推离了数据框的末尾。 I expected shift() to have a flag to change this behaviour, but I can't find anything.我希望 shift() 有一个标志来改变这种行为,但我找不到任何东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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