我有一个数据集 x 和 y。 x 是适用于所有 y 数据的单列表。使用pd.concat([pd.Series(x),pd.DataFrame(y)], axis=1)
,我希望达到以下目标:
x1 y1 ya
x2 y2 yb
x3 y3 yc
x4 y4 yd
x5 y5 ye
x6 y6 yf
- x 包含 x1,x2,x3,x4,x5,x6。
- y 包含 [y1,y2,y3,y4,y5,y6]、[ya,yb,yc,yd,ye,yf]。
- x 和 y 位于 pandas DataFrame 中。
运行 pd.concat([pd.Series(x),pd.DataFrame(y)], axis=1)
或 pd.concat([pd.DataFrame(x) ),pd.DataFrame(y)], axis=1)
结果如下
x1 Nan Nan
x2 Nan Nan
x3 y1 ya
x4 y2 yb
x5 y3 yc
x6 y4 yd
y5 ye
y6 yf
因此代码 y 值向 axis=0
方向移动了两个单元格。我在此数据框中有索引。我已经删除了索引。没有观察到差异。
只有当我连接 x 和 y 时才会观察到这种转变。
如何将 y 值向上移动 2 倍?
谢谢!
最佳答案
当 2 系列的索引未对齐时,就会发生这种情况。重置索引后尝试 (reset_index(drop=True)
),以便系列或数据帧的索引对齐:
pd.concat([pd.Series(x).reset_index(drop=True),pd.DataFrame(y)
.reset_index(drop=True)], axis=1)
或者:
pd.concat([pd.DataFrame(x).reset_index(drop=True),pd.DataFrame(y)
.reset_index(drop=True)],axis=1)
添加示例:
s1=pd.Series([1,2,3]) #normal indexed
s2=pd.Series([4,5,6],index=[2,3,4]) #index starts from 2
在axis=1
上正常连接
pd.concat([s1,s2],axis=1)
<小时/>
0 1
0 1.0 NaN
1 2.0 NaN
2 3.0 4.0
3 NaN 5.0
4 NaN 6.0
<小时/>
与reset_index()连接
pd.concat([s1.reset_index(drop=True),s2.reset_index(drop=True)],axis=1)
<小时/>
0 1
0 1 4
1 2 5
2 3 6
关于python - 使用 pd.concat(x,y) 时引起的有线移位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56403495/