合并无数据,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 21:04:15 浏览: 29
如果合并结果为空,有可能是因为两个表格中没有共同的行。可以尝试更改合并的列名或者检查数据是否有误。另外,合并方式为 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 表格和合并后的所有数据合并 result = pd.concat([result, df], ignore_index=True) # 输出到新的 Excel 表格中 df.to_excel('new_table.xlsx', index=False)
你修改的代码仍然有问题,第 8 行中的 result = pd.DataFrame() 会创建一个空 DataFrame 对象,但是在第 10-13 行的 for 循环中,你使用了 append 方法将 df 合并到 result 中,这会创建另一个新的 DataFrame 对象,result 这个空 DataFrame 对象并没有被修改。因此,在第 15 行的代码中,你再次使用了 df,这个变量并没有被定义,会导致程序运行错误。正确的代码如下:
```python
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 = 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)
```
这样修改后,代码应该可以正确地将多个 Excel 表格合并到一个 DataFrame 中,并输出到新的 Excel 表格中。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![xlsx](https://img-home.csdnimg.cn/images/20210720083732.png)