解析 Excel 报告
Parse Excel Report
我有一个 Excel 工作簿报告 (abc.xlsx),我正在尝试解析它以从几列中获取数据,我想知道如何在 Python/Pandas.该报告采用报告格式,因此它的格式不像 row-column 配置那样好,它看起来像这样:
My Report
ABC LLC
from 06/01/2015 to 06/30/2015
Instrument Identifier Quantity Start End Total
North America
XYZ 100 0 0 (1,893.52)
North America Subtotal (1,893.52)
Europe
ABC 50 10 20 (4,653.21)
Europe Subtotal (4,653.21
我感兴趣的数据是标识符(在我的 Excel 中,它位于 B 列和 C 列中,并且它们已合并)和总计(G 列)。这里的挑战是这种格式有点不规则并且带有小计线。我能想到的一种方法是使用 read_excel 通过 Pandas 读取此内容,然后遍历行并只考虑填充 B&C 和 G 列的行,但不确定如何处理这种情况headers加上合并的单元格。这种方法也很丑陋,所以我想知道是否有人在 Excel 中也有解析类似报告的经验。
有大量模块可用于解析 excel 文件。
这方面的一个例子是(我个人最喜欢的)openpyxl
模块。
一些示例语法:
wb = openpyxl.load_workbook('example.xlsx') # opens an excel workbook
wb.get_sheet_names() # get sheet names
哈哈
'Cell ' + c.coordinate + ' is ' + c.value # getting value of one
单元格
openpyxl
有大量的函数可以解析来自 excel 个文件的数据。您应该能够在这里找到实现目标的简单方法。
这里有一些很好的资源站点:
https://automatetheboringstuff.com/chapter12/
https://openpyxl.readthedocs.org/en/latest/
编码愉快!祝你好运!
我可能会这样处理。当然,它不会在所有数据修改情况下都有效,但它似乎适用于您提供的示例。
打算使用 pd.read_excel
,然后跳过导入时的前几行,将它们移开。
In [1]: import pandas as pd
In [2]: df = pd.read_excel("abc.xls", skiprows=4)
In [3]: df
Out[3]:
Instrument Identifier Quantity Start End Total
0 NaN NaN NaN NaN NaN NaN
1 North America NaN NaN NaN NaN NaN
2 NaN XYZ 100 0 0 1893.52
3 North America NaN NaN NaN Subtotal 1893.52
4 NaN NaN NaN NaN NaN NaN
5 Europe NaN NaN NaN NaN NaN
6 NaN ABC 50 10 20 4653.21
7 Europe NaN NaN NaN Subtotal 4653.21
在这里,我将使用 fillna
方法的 ffill
参数在 Instruments 列中前向填充 NaN 值。
In [4]: df.Instrument.fillna(method="ffill", inplace=True)
In [5]: df
Out[5]:
Instrument Identifier Quantity Start End Total
0 NaN NaN NaN NaN NaN NaN
1 North America NaN NaN NaN NaN NaN
2 North America XYZ 100 0 0 1893.52
3 North America NaN NaN NaN Subtotal 1893.52
4 North America NaN NaN NaN NaN NaN
5 Europe NaN NaN NaN NaN NaN
6 Europe ABC 50 10 20 4653.21
7 Europe NaN NaN NaN Subtotal 4653.21
现在只需过滤掉 Identifier
列中的空值。
In [6]: df[df.Identifier.notnull()]
Out[6]:
Instrument Identifier Quantity Start End Total
2 North America XYZ 100 0 0 1893.52
6 Europe ABC 50 10 20 4653.21
我有一个 Excel 工作簿报告 (abc.xlsx),我正在尝试解析它以从几列中获取数据,我想知道如何在 Python/Pandas.该报告采用报告格式,因此它的格式不像 row-column 配置那样好,它看起来像这样:
My Report ABC LLC from 06/01/2015 to 06/30/2015 Instrument Identifier Quantity Start End Total North America XYZ 100 0 0 (1,893.52) North America Subtotal (1,893.52) Europe ABC 50 10 20 (4,653.21) Europe Subtotal (4,653.21
我感兴趣的数据是标识符(在我的 Excel 中,它位于 B 列和 C 列中,并且它们已合并)和总计(G 列)。这里的挑战是这种格式有点不规则并且带有小计线。我能想到的一种方法是使用 read_excel 通过 Pandas 读取此内容,然后遍历行并只考虑填充 B&C 和 G 列的行,但不确定如何处理这种情况headers加上合并的单元格。这种方法也很丑陋,所以我想知道是否有人在 Excel 中也有解析类似报告的经验。
有大量模块可用于解析 excel 文件。
这方面的一个例子是(我个人最喜欢的)openpyxl
模块。
一些示例语法:
wb = openpyxl.load_workbook('example.xlsx') # opens an excel workbook
wb.get_sheet_names() # get sheet names
哈哈
'Cell ' + c.coordinate + ' is ' + c.value # getting value of one
单元格
openpyxl
有大量的函数可以解析来自 excel 个文件的数据。您应该能够在这里找到实现目标的简单方法。
这里有一些很好的资源站点:
https://automatetheboringstuff.com/chapter12/
https://openpyxl.readthedocs.org/en/latest/
编码愉快!祝你好运!
我可能会这样处理。当然,它不会在所有数据修改情况下都有效,但它似乎适用于您提供的示例。
打算使用 pd.read_excel
,然后跳过导入时的前几行,将它们移开。
In [1]: import pandas as pd
In [2]: df = pd.read_excel("abc.xls", skiprows=4)
In [3]: df
Out[3]:
Instrument Identifier Quantity Start End Total
0 NaN NaN NaN NaN NaN NaN
1 North America NaN NaN NaN NaN NaN
2 NaN XYZ 100 0 0 1893.52
3 North America NaN NaN NaN Subtotal 1893.52
4 NaN NaN NaN NaN NaN NaN
5 Europe NaN NaN NaN NaN NaN
6 NaN ABC 50 10 20 4653.21
7 Europe NaN NaN NaN Subtotal 4653.21
在这里,我将使用 fillna
方法的 ffill
参数在 Instruments 列中前向填充 NaN 值。
In [4]: df.Instrument.fillna(method="ffill", inplace=True)
In [5]: df
Out[5]:
Instrument Identifier Quantity Start End Total
0 NaN NaN NaN NaN NaN NaN
1 North America NaN NaN NaN NaN NaN
2 North America XYZ 100 0 0 1893.52
3 North America NaN NaN NaN Subtotal 1893.52
4 North America NaN NaN NaN NaN NaN
5 Europe NaN NaN NaN NaN NaN
6 Europe ABC 50 10 20 4653.21
7 Europe NaN NaN NaN Subtotal 4653.21
现在只需过滤掉 Identifier
列中的空值。
In [6]: df[df.Identifier.notnull()]
Out[6]:
Instrument Identifier Quantity Start End Total
2 North America XYZ 100 0 0 1893.52
6 Europe ABC 50 10 20 4653.21