ExcelをNumPyに変換

NumPyの紹介

NumPy(Numerical Python)はPythonのオープンソースの数値演算拡張です。このツールは大きな行列を保存および処理するために使用でき、Pythonの階層リスト構造よりも効率的です(行列を表すためにも使用できます)。多数の次元配列および行列演算をサポートし、また、配列操作のための多数の数学関数ライブラリを提供します。

NumPyの主な機能:

  1. Ndarrayは、高速で柔軟性があり、データ構造を節約する多次元配列オブジェクトです。
  2. 行列乗算、転置、逆行列などの線形代数演算。
  3. 高速フーリエ変換、配列上で高速フーリエ変換を行います。
  4. 浮動小数点配列の高速操作。
  5. 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. サンプルファイルをロードします。
  2. Aspose.Cells for Python via .NETを使用して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")
# 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. サンプルファイルをロードします。
  2. 最初のワークシートを取得します。
  3. 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を使用して範囲データをNumPy ndarrayにエクスポートする方法を示すコードスニペットの例ですvia .NET。

  1. サンプルファイルをロードします。
  2. 最初のワークシートを取得します。
  3. 範囲を作成します。
  4. 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を使用してListObjectデータをNumPy ndarrayにエクスポートする方法を示すコードスニペットの例ですvia .NET。

  1. サンプルファイルをロードします。
  2. 最初のワークシートを取得します。
  3. ListObjectオブジェクトを作成します。
  4. 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を使用して行データをNumPy ndarrayにエクスポートする方法を示すコードスニペットの例ですvia .NET。

  1. サンプルファイルをロードします。
  2. 最初のワークシートを取得します。
  3. 行インデックスで行オブジェクトを取得します。
  4. 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。

  1. サンプルファイルをロードします。
  2. 最初のワークシートを取得します。
  3. 列インデックスで列オブジェクトを取得します。
  4. 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']