将Excel转换为NumPy
NumPy简介
NumPy(Numerical Python)是Python的开源数值计算扩展。该工具可用于存储和处理大型矩阵,比Python的嵌套列表结构效率更高(也可用于表示矩阵)。它支持大量的多维数组和矩阵操作,并为数组操作提供了大量的数学函数库。
NumPy的主要功能:
- Ndarray,一个多维数组对象,是一个快速、灵活、节省空间的数据结构。
- 线性代数操作,包括矩阵乘法、转置、求逆等。
- 快速傅里叶变换,在数组上执行快速傅里叶变换。
- 浮点数组的快速操作。
- 将C语言代码集成到Python中,使其运行速度更快。
使用Aspose.Cells for Python via .NET API,您可以将Excel、TSV、CSV、Json等多种格式转换为Numpy ndarray。
将Excel工作簿转换为NumPy ndarray的方法
以下是一个示例代码片段,演示使用Aspose.Cells for Python via .NET将excel数据导出为NumPy数组的方法:
- 加载样本文件。 1.遍历excel数据并使用Aspose.Cells for Python via .NET将数据导出为NumPy ndarray。
import numpy as np | |
import aspose.cells | |
from aspose.cells import Workbook, Worksheet, Range | |
# Open the Excel workbook | |
book = Workbook("sample_data.xlsx") | |
# workbook to ndarray | |
excel_ndarray = np.array([], dtype= object) | |
sheet_count = book.worksheets.capacity - 1 | |
excel_list = [] | |
for sheet_index in range(0, sheet_count): | |
sheet_list =[] | |
sheet = book.worksheets.get(sheet_index) | |
cells = sheet.cells | |
rows = cells.rows | |
max_column_index = cells.max_column + 1 | |
row_count = rows.count | |
index = -1 | |
for row_index in range(0, row_count): | |
row = rows.get_row_by_index(row_index) | |
if row_index != row.index: | |
for blank_row_index in range(index+1, row.index): | |
blank_row =[] | |
for blank_column_index in range(0,max_column_index): | |
blank_row.append("") | |
sheet_list.append(blank_row) | |
data_row =[] | |
for column_index in range(0,max_column_index): | |
curr_cell = cells.check_cell(row.index, column_index) | |
if curr_cell: | |
data_row.append(curr_cell.value) | |
else: | |
data_row.append("") | |
sheet_list.append(data_row) | |
index = row.index | |
excel_list.append(sheet_list) | |
excel_ndarray = np.asarray(excel_list) | |
print(excel_ndarray) |
输出结果:
[[['City' 'Region' 'Store']
['Chicago' 'Central' '3055']
['New York' 'East' '3036']
['Detroit' 'Central' '3074']]
[['City2' 'Region2' 'Store3']
['Seattle' 'West' '3000']
['philadelph' 'East' '3082']
['Detroit' 'Central' '3074']]
[['City3' 'Region3' 'Store3']
['Seattle' 'West' '3166']
['New York' 'East' '3090']
['Chicago' 'Central' '3055']]]
将工作表转换为NumPy ndarray的方法
以下是一个示例代码片段,演示使用Aspose.Cells for Python via .NET将工作表数据导出为NumPy ndarray的方法:
- 加载样本文件。
- 获取第一个工作表。 1.使用Aspose.Cells for Python Excel库将工作表数据转换为Numpy ndarray。
import numpy as np | |
import aspose.cells | |
from aspose.cells import Workbook, Worksheet, Range | |
# Open the Excel workbook | |
book = Workbook("sample_data.xlsx") | |
# Get the first worksheet | |
sheet1 = book.worksheets.get(0) | |
cells = sheet1.cells | |
rows = cells.rows | |
max_column_index = sheet1.cells.max_column + 1 | |
# worksheet to ndarray | |
worksheet_list =[] | |
row_count = rows.count | |
index = -1 | |
for row_index in range(0, row_count): | |
row = rows.get_row_by_index(row_index) | |
if row_index != row.index: | |
for blank_row_index in range(index+1, row.index): | |
blank_row =[] | |
for blank_column_index in range(0,max_column_index): | |
blank_row.append("") | |
worksheet_list.append(blank_row) | |
data_row =[] | |
for column_index in range(0,max_column_index): | |
curr_cell = cells.check_cell(row.index, column_index) | |
if curr_cell: | |
data_row.append(curr_cell.value) | |
else: | |
data_row.append("") | |
worksheet_list.append(data_row) | |
index = row.index | |
worksheet_ndarray = np.asarray(worksheet_list) | |
print(worksheet_ndarray) |
输出结果:
[['City' 'Region' 'Store']
['Chicago' 'Central' '3055']
['New York' 'East' '3036']
['Detroit' 'Central' '3074']]
如何将Excel的范围转换为NumPy ndarray
下面是一个示例代码片段,演示如何使用Aspose.Cells for Python via .NET将范围数据导出到NumPy ndarray中:
- 加载样本文件。
- 获取第一个工作表。
- 创建范围。
- 使用Aspose.Cells for Python Excel库将范围数据转换为NumPy ndarray。
import numpy as np | |
import aspose.cells | |
from aspose.cells import Workbook, Worksheet, Range | |
# Open the Excel workbook | |
book = Workbook("sample_data.xlsx") | |
# Get the first worksheet | |
sheet1 = book.worksheets.get(0) | |
# range to ndarray | |
cells = sheet1.cells | |
range_obj = cells.create_range("B1", "C3") | |
range_list =[] | |
for row_index in range(range_obj.first_row , range_obj.first_row + range_obj.row_count ): | |
row =[] | |
for column_index in range(range_obj.first_column, range_obj.first_column + range_obj.column_count): | |
curr_cell = cells.check_cell(row_index, column_index) | |
if curr_cell: | |
row.append(curr_cell.value) | |
else: | |
row.append("") | |
range_list.append(row) | |
range_ndarray = np.asarray(range_list) | |
print(range_ndarray) |
输出结果:
[['Region' 'Store']
['Central' '3055']
['East' '3036']]
如何将Excel的ListObject转换为NumPy ndarray
下面是一个示例代码片段,演示如何使用Aspose.Cells for Python via .NET将ListObject数据导出为NumPy ndarray:
- 加载样本文件。
- 获取第一个工作表。
- 创建ListObject对象。
- 使用Aspose.Cells for Python Excel库将ListObject数据转换为NumPy ndarray。
import numpy as np | |
import aspose.cells | |
from aspose.cells import Workbook, Worksheet, Range | |
# Open the Excel workbook | |
book = Workbook("sample_data.xlsx") | |
# Get the first worksheet | |
sheet1 = book.worksheets.get(0) | |
cells = sheet1.cells | |
# listobject to ndarray | |
table_index = sheet1.list_objects.add("A1", "C4", True) | |
table = sheet1.list_objects[table_index] | |
table_list =[] | |
for row_index in range(table.start_row , table.end_row + 1): | |
row =[] | |
for column_index in range(table.start_column, table.end_column + 1): | |
curr_cell = cells.check_cell(row_index, column_index) | |
if curr_cell: | |
row.append(curr_cell.value) | |
else: | |
row.append("") | |
table_list.append(row) | |
table_ndarray = np.asarray(table_list) | |
print(table_ndarray) |
输出结果:
[['City' 'Region' 'Store']
['Chicago' 'Central' '3055']
['New York' 'East' '3036']
['Detroit' 'Central' '3074']]
如何将Excel的行转换为NumPy ndarray
下面是一个示例代码片段,演示如何使用Aspose.Cells for Python via .NET将行数据导出为NumPy ndarray:
- 加载样本文件。
- 获取第一个工作表。
- 通过行索引获取行对象。
- 使用Aspose.Cells for Python Excel库将行数据转换为NumPy ndarray。
import numpy as np | |
import aspose.cells | |
from aspose.cells import Workbook, Worksheet, Range | |
# Open the Excel workbook | |
book = Workbook("sample_data.xlsx") | |
# Get the first worksheet | |
sheet1 = book.worksheets.get(0) | |
cells = sheet1.cells | |
max_column_index = cells.max_column + 1 | |
# row to ndarray | |
row_index = cells.max_data_row | |
row_list = [] | |
for column_index in range(0,max_column_index): | |
curr_cell = cells.check_cell(row_index, column_index) | |
if curr_cell: | |
row_list.append(curr_cell.value) | |
else: | |
row_list.append("") | |
row_ndarray = np.asarray(row_list) | |
print(row_ndarray) |
输出结果:
['Detroit' 'Central' '3074']
如何将Excel的列转换为NumPy ndarray
以下是一个示例代码片段,演示如何使用Aspose.Cells for Python将列数据导出为NumPy ndarray via .NET:
- 加载样本文件。
- 获取第一个工作表。
- 通过列索引获取列对象。
- 使用Aspose.Cells for Python Excel库将列数据转换为NumPy ndarray。
import numpy as np | |
import aspose.cells | |
from aspose.cells import Workbook, Worksheet, Range | |
# Open the Excel workbook | |
book = Workbook("sample_data.xlsx") | |
# Get the first worksheet | |
sheet1 = book.worksheets.get(0) | |
cells = sheet1.cells | |
max_row_index = cells.max_row + 1 | |
# column to ndarray | |
column_index = cells.max_data_column | |
column_list = [] | |
for row_index in range(0,max_row_index): | |
curr_cell = sheet1.cells.check_cell(row_index, column_index) | |
if curr_cell: | |
column_list.append(curr_cell.value) | |
else: | |
column_list.append("") | |
column_ndarray = np.asarray(column_list) | |
print(column_ndarray) |
输出结果:
['Store' '3055' '3036' '3074']