为什么熊猫read_excel移动列与每次迭代?



我正在尝试阅读一堆excel文件(700+),并使用for循环将它们编译成一个数据库。但是,for循环的每次迭代都以一种奇怪的重复模式将前四列移到数据集的末尾。我没有使用过python,我不知道是什么原因造成的。

excel_files = glob.glob("/State Report_2020****308.xls")
list1 = [pd.read_excel(filename, sheet_name="Raw Data", usecols = "A:S", skiprows = 9, nrows=33-10) for filename in excel_files]
raw_data = pd.concat(list1, axis=0, ignore_index=True)
例如,从第一个工作表中提取的数据如下:
A B C ... Q R S
a b c ... q r s 
a b c ... q r s
a b c ... q r s
a b c ... q r s
然后从第二个工作表中提取数据,附加到数据框架的底部,看起来像:
A B C ... Q R S T U V W
e f g ... q r S a b c d
e f g ... q r S a b c d
e f g ... q r S a b c d
e f g ... q r S a b c d

然后第三个表中的数据迭代到数据框架中,看起来像:

A B C ... Q R S T U V W X Y Z AA
e f g ... q r S         a b c d
e f g ... q r S         a b c d
e f g ... q r S         a b c d
e f g ... q r S         a b c d

此模式在每次迭代中将数据的第一列进一步向右移动时重复。

错误来自"skiprows = 9"作为警署的一员。read_excel输入。第9行是表的列标题。我想如果我把标题留在那里,它们将作为一行添加到每个后续迭代中,如:

A B C ... Q R S
a b c ... q r s 
a b c ... q r s
...............
A B C ... Q R S
a b c ... q r s 
a b c ... q r s
...............
A B C ... Q R S
a b c ... q r s 
a b c ... q r s
...............

我把标题留在"skiprows = 8"并得到了我想要的结果。

A B C ... Q R S
a b c ... q r s 
a b c ... q r s
...............
a b c ... q r s 
a b c ... q r s
...............
a b c ... q r s 
a b c ... q r s
...............

最新更新