python pandas合并excel表不起作用

时间:2016-07-13 09:40:50

标签: excel pandas python-3.5

我尝试使用常见字段串行合并两个excel表,但却抛出了一些错误。我的计划如下:

    (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')

收到如下错误:

localhost:81/posrkidev/web/index.php/inventory/create

我在下面的SO链接中提到了这个问题: .replace() method on the request

2 个答案:

答案 0 :(得分:1)

这是因为加入后列名重叠。您可以将索引设置为Serial并加入,或在rsuffix=函数中指定lsuffix=join值,以便将后缀值附加到公共列名。

答案 1 :(得分:1)

小修改对我有用,

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')