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

标签 python pandas dataframe concatenation

我有 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/

相关文章:

python - 如何在 matplotlib 生成的 PDF 中嵌入字体?

python - 在列上应用 sqrt 函数

python - 如何使用 conda 环境为 anaconda spyder 设置不同的桌面启动器?

python - 在多个文件中编写同一行的简洁方法 - Python

Python Pandas 混合 bool Yes/True 和 NaN 列

python - pandas 在数据帧之间交换行

r - 删除 | 之后的所有数据在数据栏中

python - 我如何在 Go 中做类似 numpy arange 的事情?

python - 删除与 python 中的某些模式匹配的列