简体   繁体   English

用pandas附加两个数据框

[英]append two data frame with pandas

When I try to merge two dataframes by rows doing: 当我尝试按行合并两个数据帧时:

bigdata = data1.append(data2)

I get the following error: 我收到以下错误:

 Exception: Index cannot contain duplicate values! 

The index of the first data frame starts from 0 to 38 and the second one from 0 to 48. I didn't understand that I have to modify the index of one of the data frame before merging, but I don't know how to. 第一个数据帧的索引从0到38开始,第二个从0到48开始。我不明白我必须在合并之前修改其中一个数据帧的索引,但我不知道如何。

Thank you. 谢谢。

These are the two dataframes: 这是两个数据帧:

data1 : data1

    meta  particle  ratio   area    type    
0   2     part10    1.348   0.8365  touching
1   2     part18    1.558   0.8244  single  
2   2     part2     1.893   0.894   single  
3   2     part37    0.6695  1.005   single  
....clip...
36  2     part23    1.051   0.8781  single  
37  2     part3     80.54   0.9714  nuclei  
38  2     part34    1.071   0.9337  single  

data2 : data2

    meta  particle  ratio    area    type    
0   3     part10    0.4756   1.025   single  
1   3     part18    0.04387  1.232   dusts   
2   3     part2     1.132    0.8927  single  
...clip...
46  3     part46    13.71    1.001   nuclei  
47  3     part3     0.7439   0.9038  single  
48  3     part34    0.4349   0.9956  single 

the first column is the index 第一列是索引

append函数有一个可选参数ignore_index ,你应该在这里使用它来连接记录,因为索引对你的应用程序没有意义。

You could first identify the index-duplicated (not value) row using groupby method, and then do a sum/mean operation on all the rows with the duplicate index. 您可以首先使用groupby方法识别索引复制(非值)行,然后对具有重复索引的所有行执行求和/均值操作。

data1 = data1.groupby(data1.index).sum()
data2 = data2.groupby(data2.index).sum()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM