【问题标题】:Pandas concat function gives ValueError : Shape of passed values is {passed}, indices imply {implied}Pandas concat 函数给出 ValueError :传递值的形状是 {passed},索引暗示 {implied}
【发布时间】:2020-05-24 12:16:59
【问题描述】:
result = df1.append(df2)
finalDf = pd.concat([principal_Df, result[['label']]], axis=1)

print(principal_Df.shape) //gives (12390, 5)
print(result.shape) // gives (12390, 9)

连接线给出

raise ValueError(f"传递值的形状是 {passed}, 索引暗示 {implied}") ValueError:传递值的形状为 (18585, 6),索引 暗示 (12390, 6)

我不明白为什么它说 18585。还有其他方法可以连接吗?请帮忙。

编辑: 我想我找到了问题所在。

打印结果如下

      label
0       1.0
1       1.0
2       1.0
3       1.0
4       1.0
     ...
6190    0.0
6191    0.0
6192    0.0
6193    0.0
6194    0.0
[12390 rows x 1 columns]

打印 principal_Df 给出

      principal component 1  ...  principal component 5
0                  -3.815308  ...              -0.921742
1                  -0.192024  ...              -0.449291
2                  -1.755755  ...               0.603834
3                  -0.663780  ...               0.711707
4                   1.288255  ...               1.115953
                      ...  ...                    ...
12385               0.819819  ...               0.534367
12386               1.343206  ...               0.153296
12387               2.327933  ...              -1.012771
12388              -0.180687  ...              -0.048978
12389              -0.240281  ...              -0.042431
[12390 rows x 5 columns]

结果原来是通过追加两个df得到的

result = df1.append(df2)

并且行号不是从 0 到 12390 的延续,而是从 0 到 6194 并在 0 到 6194 处重新开始。这可能是问题吗? 如何让 result 的行索引在 df.append 上继续?

【问题讨论】:

  • result[['label']] 可能是这个双重[] 导致问题
  • 这是一个错字,但事实并非如此。改变没有帮助。

标签: python python-3.x pandas dataframe valueerror


【解决方案1】:

我想通了。我必须做result = df1.append(df2, ignore_index=True),然后它会继续索引。问题解决了。

【讨论】:

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