简体   繁体   English

pandas 移位的列名

[英]Column names with pandas shift

Trying to create lags for pandas columns with column_names尝试使用column_names为 pandas 列创建滞后

Sample DF Code:示例 DF 代码:

df = pd.DataFrame(np.random.randint(0,10,size=(4,2)))
df.shift(1)

OP:操作:

     0   1
0   NaN NaN
1   9.0 2.0
2   4.0 5.0
3   6.0 0.0

but when I try to create this with column names, i get nan但是当我尝试用列名创建它时,我得到了nan

df1=pd.DataFrame(df.shift(1),columns=["lag"+str(each) for each in df.columns])
df1

OP:操作:

    lag0  lag1
0   NaN   NaN
1   NaN   NaN
2   NaN   NaN
3   NaN   NaN

Any suggestion to rectify this?有什么建议可以纠正这个问题吗?

Here's another approach:这是另一种方法:

df = df.shift(1)

l = list(df.columns.astype('str'))
s = 'lag'
cols = [s + i for i in l]
df.columns = cols

df
    lag0    lag1
0   NaN     NaN
1   7.0     4.0
2   4.0     8.0
3   0.0     9.0

Problem is there are different columns names, so after created new DataFrame columns names not matched and are created misisng values, it is called index alignmenet.问题是有不同的列名,所以在创建新的 DataFrame 列名不匹配并且创建了错误值之后,它被称为索引对齐。

For prevent it is possible convert values to numpy array:为防止可能将值转换为 numpy 数组:

df1=pd.DataFrame(df.shift(1).to_numpy(),columns=["lag"+str(each) for each in df.columns])
print (df1)
   lag0  lag1
0   NaN   NaN
1   2.0   2.0
2   8.0   3.0
3   6.0   8.0

But simplier is use DataFrame.add_prefix :但更简单的是使用DataFrame.add_prefix

df1 = df.shift().add_prefix('lag')
print (df1)
   lag0  lag1
0   NaN   NaN
1   1.0   1.0
2   8.0   3.0
3   0.0   4.0

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

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