python - 使用 pd.concat(x,y) 时引起的有线移位

标签 python python-3.x pandas concatenation

我有一个数据集 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/

相关文章:

python - Python 中 str 的静态方法与实例方法

python - 如何使用 matplotlibs 颜色图 alpha 值使三色图淡入透明?

python - 合并返回奇数长度

python - 将连续的重复字符替换为一个 - 逐列操作 - `pandas.DataFrame`

python - 带 if 条件和不带 if 条件的切片

python - range 和 len 函数的替代方案

python - 使用 pandas 数据透视 reshape 两列数据

python - 具有多索引的 Pandas 长格式到宽格式

python - 混沌排列

python - 为什么从生成器内调用模拟对象无法正确测试?