编程技术网

关注微信公众号,定时推送前沿、专业、深度的编程技术资料。

 找回密码
 立即注册

QQ登录

只需一步,快速开始

极客时间

python合并Excel工作表的 pandas 无法正常工作:python pandas merging excel sheets not working

Faruque Ahamed Python 2022-5-11 10:50 4人围观

腾讯云服务器
python合并Excel工作表的 pandas 无法正常工作的处理方法

我正在尝试使用常见的序列号合并两个Excel工作表,但是会引发一些错误.我的程序如下:

I'm trying to merge two excel sheets using the common filed Serial but throwing some errors. My program is as below :

 (user1_env)root@ubuntu:~/user1/test/compare_files# cat compare.py import pandas as pd source1_df = pd.read_excel('a.xlsx', sheetname='source1') source2_df = pd.read_excel('a.xlsx', sheetname='source2') joined_df = source1_df.join(source2_df, on='Serial') joined_df.to_excel('/root/user1/test/compare_files/result.xlsx') 

出现如下错误:

 (user1_env)root@ubuntu:~/user1/test/compare_files# python3.5 compare.py Traceback (most recent call last): File "compare.py", line 5, in <module> joined_df = source1_df.join(source2_df, on='Serial') File "/home/user1/miniconda3/envs/user1_env/lib/python3.5/site-packages/pandas/core/frame.py", line 4385, in join rsuffix=rsuffix, sort=sort) File "/home/user1/miniconda3/envs/user1_env/lib/python3.5/site-packages/pandas/core/frame.py", line 4399, in _join_compat suffixes=(lsuffix, rsuffix), sort=sort) File "/home/user1/miniconda3/envs/user1_env/lib/python3.5/site-packages/pandas/tools/merge.py", line 39, in merge return op.get_result() File "/home/user1/miniconda3/envs/user1_env/lib/python3.5/site-packages/pandas/tools/merge.py", line 223, in get_result rdata.items, rsuf) File "/home/user1/miniconda3/envs/user1_env/lib/python3.5/site-packages/pandas/core/internals.py", line 4445, in items_overlap_with_suffix to_rename) ValueError: columns overlap but no suffix specified: Index(['Serial'], dtype='object') 

我是在下面的SO链接中提及该问题: python比较两个Excel工作表并附加正确的记录

I'm referring below SO link for the issue : python compare two excel sheet and append correct record

问题解答

小的修改对我有用,

import pandas as pd source1_df = pd.read_excel('a.xlsx', sheetname='source1') source2_df = pd.read_excel('a.xlsx', sheetname='source2') joined_df = pd.merge(source1_df,source2_df,on='Serial',how='outer') joined_df.to_excel('/home/gk/test/result.xlsx') 

这篇关于python合并Excel工作表的 pandas 无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程技术网(www.editcode.net)!

腾讯云服务器 阿里云服务器
关注微信
^