gpt4 book ai didi

python - 绕过pandas concat错误 "Reindexing only valid with uniquely valued Index objects"

转载 作者:太空宇宙 更新时间:2023-11-03 14:58:52 24 4
gpt4 key购买 nike

我有 3 个数据帧,包括来自同一组的信息,现在我尝试按组名称set_index 连接这些数据帧,但是因为 df1 包含不唯一的索引,因此我无法将它们连接起来。有什么办法可以绕过它吗?

输入 df 的样本:

df1:
group A B
cat 1 0
cat 2 7
cat 5 5
dog 0.4 1
dog 2 4
dog 8 7
seal 7 5
seal 1 8
seal 7 9

df2:
group C D
cat 1 3
seal 0 5
dog 3 4

df3:
group E F
cat 1 5
dog 0 3
seal 5 9

想要的输出:

group     A       B       C        D       E      F
cat 1 0 1 3 1 5
cat 2 7 1 3 1 5
cat 5 5 1 3 1 5
dog 0.4 1 3 4 0 3
dog 2 4 3 4 0 3
dog 8 7 3 4 0 3
seal 7 5 0 5 5 9
seal 1 8 0 5 5 9
seal 7 9 0 5 5 9

我的代码:

 df1 = pd.read(file).set_index('group')
df2 = pd.read(file).set_index('group')
df3 = pd.read(file).set_index('group')

all_data = pd.concate(df1, df2, df3, axis = 1).reset_index()

错误:

 pandas.core.indexes.base.InvalidIndexError: Reindexing only valid with uniquely valued Index objects

谢谢!

最佳答案

我认为你可以使用concat如果尺寸相同,则首先是 df2df3 的,然后是 join :

df = pd.concat([df2.set_index('group'), df3.set_index('group')], axis = 1)
all_data = df1.join(df, on='group')
print (all_data)
group A B C D E F
0 cat 1.0 0 1 3 1 5
1 cat 2.0 7 1 3 1 5
2 cat 5.0 5 1 3 1 5
3 dog 0.4 1 3 4 0 3
4 dog 2.0 4 3 4 0 3
5 dog 8.0 7 3 4 0 3
6 seal 7.0 5 0 5 5 9
7 seal 1.0 8 0 5 5 9
8 seal 7.0 9 0 5 5 9

也可以在read_csv中使用参数index_col相反 set_index :

df1 = pd.read(file)
df2 = pd.read(file, index_col='group')
df3 = pd.read(file, index_col='group')

df = pd.concat([df2, df3], axis = 1)
all_data = df1.join(df, on='group')

关于python - 绕过pandas concat错误 "Reindexing only valid with uniquely valued Index objects",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45294446/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com