【问题标题】:Python Pandas: Shape of passed values is (126, 5), indices imply (84, 5) [duplicate]Python Pandas:传递值的形状为 (126, 5),索引暗示 (84, 5) [重复]
【发布时间】:2020-11-06 07:01:31
【问题描述】:

我有 2 个 84 行的数据帧,长度明显相同,但是当我想将它们连接到 1 个 df(按列连接 - 将名称、边缘和偏移量放在纬度和经度的右侧)时,我得到了这个错误,。

发生了什么事?

     Latitude  Longitude
0   45.403538 -75.735729
1   45.403506 -75.735699
2   45.409095 -75.722588
3   45.409069 -75.722552
4   45.413496 -75.714184
..        ...        ...
79  45.415609 -75.644769
80  45.416073 -75.645726
81  45.416193 -75.638802
82  45.416172 -75.638223

[84 rows x 2 columns]

  name Edge  Offset
0    TUN-W    1    3000
1    TUN-E    2    3000
2    BAY-W    5  102510
3    BAY-E    6  102579
4    PIM-W    5  186035
..     ...  ...     ...
37  PTSTTW   33   52710
38  PTSTTE   34   18997
39   PAG11   40   24362
40   PAG14   50    9927
41  PHND15  177   11662

[84 rows x 3 columns]

这是我的代码衬里

output_df = pd.concat( [output_df, input_df], axis=1)

【问题讨论】:

  • output_df = pd.concat( [output_df.reset_index(drop=True), input_df.reset_index(drop=True)], axis=1) 可以接受吗?
  • 是的,我对其他堆栈帖子进行了所有研究,但那些 egs 真的很复杂。我只需要简单的因为我知道我自己的组件 dfs 可以打印出来。所以应该没那么复杂

标签: python pandas


【解决方案1】:

我明白了:

 name Edge  Offset
0    TUN-W    1    3000
1    TUN-E    2    3000
2    BAY-W    5  102510
3    BAY-E    6  102579
4    PIM-W    5  186035
..     ...  ...     ...
37  PTSTTW   33   52710
38  PTSTTE   34   18997
39   PAG11   40   24362
40   PAG14   50    9927
41  PHND15  177   11662

索引搞砸了。在中间某处,索引重置为0,而不是计数到84

所以我做了一个segments_df = segments_df_start.append(segments_df_end).reset_index()(在我的代码的早期部分)来修复该数据帧的索引,然后再传递它。

因此,在进行故障排除时,请务必检查您的索引和 .reset_index()

【讨论】:

    猜你喜欢
    • 2019-01-27
    • 2019-02-01
    • 2022-08-08
    • 2015-02-27
    • 1970-01-01
    • 1970-01-01
    • 2013-11-09
    • 2020-01-12
    相关资源
    最近更新 更多