Convertir Excel en NumPy

Introduction à NumPy

NumPy (Numerical Python) est une extension de calcul numérique open-source de Python. Cet outil peut être utilisé pour stocker et traiter de grandes matrices, ce qui est beaucoup plus efficace que la structure de listes imbriquées de Python (qui peut également être utilisée pour représenter des matrices). Il prend en charge un grand nombre de tableaux dimensionnels et d’opérations matricielles, et fournit également un grand nombre de bibliothèques de fonctions mathématiques pour les opérations sur les tableaux.

Les principales fonctions de NumPy :

  1. Ndarray, un objet de tableau multidimensionnel, est une structure de données rapide, flexible et économique en espace.
  2. Opérations d’algèbre linéaire, y compris la multiplication de matrices, la transposition, l’inversion, etc.
  3. Transformation de Fourier, effectuant une transformation de Fourier rapide sur un tableau.
  4. Opération rapide des tableaux à virgule flottante.
  5. Intégrer du code en langage C dans Python pour le faire fonctionner plus rapidement.

En utilisant l’API Aspose.Cells pour Python via .NET, vous pouvez convertir des fichiers Excel, TSV, CSV, Json et de nombreux autres formats en tableau Numpy.

Comment convertir un classeur Excel en tableau Numpy

Voici un exemple de code pour montrer comment exporter des données Excel vers un tableau NumPy en utilisant Aspose.Cells pour Python via .NET:

  1. Charger le fichier d’exemple.
  2. Parcourir les données Excel et exporter les données en tableau Numpy en utilisant Aspose.Cells pour Python via .NET.
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)

Le résultat en sortie :

[[['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']]]

Comment convertir une feuille de calcul en tableau Numpy

Voici un exemple de code pour montrer comment exporter des données de feuille de calcul en tableau Numpy en utilisant Aspose.Cells pour Python via .NET:

  1. Charger le fichier d’exemple.
  2. Obtenir la première feuille de calcul.
  3. Convertir les données de la feuille de calcul en tableau Numpy en utilisant la bibliothèque Excel Aspose.Cells pour Python.
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)

Le résultat en sortie :

[['City' 'Region' 'Store']    
 ['Chicago' 'Central' '3055'] 
 ['New York' 'East' '3036']   
 ['Detroit' 'Central' '3074']]

Comment convertir une plage d’Excel en tableau Numpy

Voici un exemple de code pour montrer comment exporter des données de plage en tableau Numpy en utilisant Aspose.Cells pour Python via .NET:

  1. Charger le fichier d’exemple.
  2. Obtenir la première feuille de calcul.
  3. Créer la plage.
  4. Convertir les données de la plage en tableau Numpy en utilisant la bibliothèque Excel Aspose.Cells pour Python.
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)

Le résultat en sortie :

[['Region' 'Store']
 ['Central' '3055']
 ['East' '3036']]

Comment convertir un ListObject d’Excel en NumPy ndarray

Voici un exemple de code pour démontrer comment exporter les données ListObject vers un NumPy ndarray en utilisant Aspose.Cells pour Python via .NET :

  1. Charger le fichier d’exemple.
  2. Obtenir la première feuille de calcul.
  3. Créer un objet ListObject.
  4. Convertir les données ListObject en NumPy ndarray en utilisant la bibliothèque Aspose.Cells pour Python Excel.
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)

Le résultat en sortie :

[['City' 'Region' 'Store']
 ['Chicago' 'Central' '3055']
 ['New York' 'East' '3036']
 ['Detroit' 'Central' '3074']]

Comment convertir une ligne d’Excel en NumPy ndarray

Voici un exemple de code pour démontrer comment exporter les données de ligne vers un NumPy ndarray en utilisant Aspose.Cells pour Python via .NET :

  1. Charger le fichier d’exemple.
  2. Obtenir la première feuille de calcul.
  3. Obtenir un objet Row par indice de ligne.
  4. Convertir les données de la ligne en NumPy ndarray en utilisant la bibliothèque Aspose.Cells pour Python Excel.
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)

Le résultat en sortie :

['Detroit' 'Central' '3074']

Comment convertir une colonne d’Excel en NumPy ndarray

Voici un exemple de code pour démontrer comment exporter les données de colonne vers un NumPy ndarray en utilisant Aspose.Cells pour Python via .NET :

  1. Charger le fichier d’exemple.
  2. Obtenir la première feuille de calcul.
  3. Obtenir un objet Column par indice de colonne.
  4. Convertir les données de la colonne en NumPy ndarray en utilisant la bibliothèque Aspose.Cells pour Python Excel.
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)

Le résultat en sortie :

['Store' '3055' '3036' '3074']