合并无数据,import pandas as pd # 读取第一个 Excel 表格的数据 df1 = pd.read_excel('附件一:2022年12月末存量公司客户管户认领情况表__海沧-1.xlsx') # 定义一个空的 DataFrame,用于存储所有 Excel 表格的数据 result = pd.DataFrame() # 循环读取其他 Excel 表格的数据,合并到 result 中 for file in ['附件二:2023年3月末新拓公司客户管户认领情况表__海沧-1.xlsx']: df = pd.read_excel(file) result = pd.concat([result, df], ignore_index=True) # 将第一个 Excel 表格和合并后的所有数据合并 df = pd.merge(df1, result, on=['所属管辖行','网点名称','客户编号','客户名称','一级支行对公部/网点维护标识','管户人员统一认证号','管户人员','管户人员岗位','管户权重']) # 输出到新的 Excel 表格中 df.to_excel('new_table.xlsx', index=False)
时间: 2023-11-25 15:04:15 浏览: 71
如果合并结果为空,有可能是因为两个表格中没有共同的行。可以尝试更改合并的列名或者检查数据是否有误。另外,合并方式为 inner join,如果需要保留所有行,可以指定合并方式为 outer join。可以尝试修改代码如下:
```
df = pd.merge(df1, result, on=['所属管辖行','网点名称','客户编号','客户名称','一级支行对公部/网点维护标识','管户人员统一认证号','管户人员','管户人员岗位','管户权重'], how='outer')
```
相关问题
数据合并后,第二个表格数据错位:import pandas as pd # 读取第一个 Excel 表格的数据 df1 = pd.read_excel('附件一:2022年12月末存量公司客户管户认领情况表__海沧-1.xlsx') # 定义一个空的 DataFrame,用于存储所有 Excel 表格的数据 result = pd.DataFrame() # 循环读取其他 Excel 表格的数据,合并到 result 中 for file in ['附件二:2023年3月末新拓公司客户管户认领情况表__海沧-1.xlsx']: df = pd.read_excel(file) result = pd.concat([result, df], ignore_index=True) # 将第一个 Excel 表格和合并后的所有数据合并 df = pd.concat([df1, result], ignore_index=True) # 输出到新的 Excel 表格中 df.to_excel('new_table.xlsx', index=False)
如果第二个表格数据错位,可能是由于该表格的列名与第一个表格的列名不一致导致的。你可以使用 pandas 的 merge 函数,指定合并列的名称,以避免数据错位。例如:
```python
import pandas as pd
# 读取第一个 Excel 表格的数据
df1 = pd.read_excel('附件一:2022年12月末存量公司客户管户认领情况表__海沧-1.xlsx')
# 读取第二个 Excel 表格的数据
df2 = pd.read_excel('附件二:2023年3月末新拓公司客户管户认领情况表__海沧-1.xlsx')
# 合并两个表格的数据,指定合并列的名称
df = pd.merge(df1, df2, on='客户编号')
# 输出到新的 Excel 表格中
df.to_excel('new_table.xlsx', index=False)
```
在上面的代码中,我们使用了 merge 函数来合并两个表格的数据,指定了合并列的名称为客户编号(假设该列在两个表格中的名称都为“客户编号”)。这样可以确保数据合并时不会出现错位的情况。
代码如下:import pandas as pd # 读取第一个 Excel 表格的数据 df1 = pd.read_excel('附件一:2022年12月末存量公司客户管户认领情况表__海沧-1.xlsx') # 定义一个空的 DataFrame,用于存储所有 Excel 表格的数据 result = pd.DataFrame() # 循环读取其他 Excel 表格的数据,合并到 result 中 for file in ['附件三:2022年12月末存量机构客户管户认领情况表__海沧支行-1.xlsx', '附件二:2023年3月末新拓公司客户管户认领情况表__海沧-1.xlsx', '附件四:2023年3月末新拓机构客户管户认领情况表__海沧支行-1.xlsx']: df = pd.read_excel(file) result = result.append(df) # 将第一个 Excel 表格和合并后的所有数据合并 df = pd.concat([df1, result], ignore_index=True) # 输出到新的 Excel 表格中 df.to_excel('new_table.xlsx', index=False)
我看了你的代码,发现问题出在第 9 行,你使用了 result.append(df) 方法,但是 DataFrame 对象并没有 append 方法。你可以使用 concat 方法将 df 合并到 result 中,如下所示:
```python
result = pd.concat([result, df], ignore_index=True)
```
这样就可以正确地将多个 Excel 表格合并到一个 DataFrame 中了。
阅读全文
相关推荐
















