Pandas concat 函数给出 ValueError :传递值的形状为 {passed},索引暗示 {implied}

问题描述 投票:0回答:1
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 最初是通过附加两个 df 得到的

result = df1.append(df2)

行号不是从 0 到 12390 的连续,而是从 0 到 6194,并从 0 到 6194 重新开始。这可能是问题所在吗? 如何获取 result 的行索引以继续 df.append?

python python-3.x pandas dataframe valueerror
1个回答
0
投票

我想通了。我必须做

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

© www.soinside.com 2019 - 2024. All rights reserved.