python - Pandas合并两个excel中的数据

标签 python excel python-3.x pandas

我有两个excel

Excel 1

files   language    blank   comment code
15       C++            66  35      354
1       C/C++ Header    3   7        4

Excel 2

files   language    blank   comment code
16       C++           33   35      354
1       C/C++ Header    3   7        4
1       Python          1   1        1

尝试合并Excel

files   language    blank   comment code
31       C++           99   70      708
2       C/C++ Header    6   14       8
1       Python          1   1        1

关于 pandas 的任何提示

最佳答案

使用concat聚合 sum by groupby :

df = pd.concat([df1, df2]).groupby('language', as_index=False).sum()
print (df)
       language  files  blank  comment  code
0           C++     31     99       70   708
1  C/C++ Header      2      6       14     8
2        Python      1      1        1     1

如果列的顺序很重要,请添加 reindex :

df=pd.concat([df1, df2]).groupby('language',as_index=False).sum().reindex(columns=df1.columns)
print (df)
   files      language  blank  comment  code
0     31           C++     99       70   708
1      2  C/C++ Header      6       14     8
2      1        Python      1        1     1

关于python - Pandas合并两个excel中的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51346119/

相关文章:

python - 每个值都取决于另一个 df 查询的 Pandas 列

python - 有没有办法在嵌入之前将用户 ID 转换为用户名?

python-3.x - 从时尚 MNIST 数据打印图像给出彩色图像?

excel - 在非单元格引用的 Excel 数组的每一行或每一列中查找最大值

excel - 如何计算特定范围内的行数?

python - pandas 中的 groupby 和函数调用

Python:使用列表值添加到字典的最佳方式

VBA在列和列表行号中查找重复项?

Python 和 Pandas - 按日期排序

python - Django - 在不同通用 View 中设置和访问 session 变量