我试图合并所有excel文件,它们都只包含1列。但是,合并的数据在下一列中被移动。有人知道怎么解决这个问题吗?下面是Python代码:
import glob
import pandas as pd
file_path = "c:\merge-excel"
files = (file_path+"/*.xlsx")
all_files = glob.glob(files)
merged_data = pd.concat([pd.read_excel(a) for a in all_files])
merged_data.to_excel (file_path+"/Merged_files.xlsx", index=False, encoding='utf-8-sig')文件1.xlsx:
1
2
3
4
5
6
7 文件2.xlsx:
8
9
10
11
12
13
14
15文件3.xlsx:
16
17
18
19
20
21
22
23Merged_files.xlsx:
1 | 8 | 16
2
3
4
5
6
7
9
10
11
12
13
14
15
17
18
19
20
21
22
23发布于 2022-10-13 00:05:05
您的问题是,header参数对read_excel的默认值是0,这意味着它在每个excel文件中取第一行,并将其用作列标题。因此,您有3列1、8和16,它们是每个文件中第一行的值。使用
pd.read_excel(a, header=None)来解决这个问题。
https://stackoverflow.com/questions/74049091
复制相似问题