Convertire Excel in NumPy

Introduzione a NumPy

NumPy (Numerical Python) è un’estensione open source per calcoli numerici di Python. Questo strumento può essere utilizzato per memorizzare e elaborare grandi matrici, molto più efficiente della struttura di liste nidificate di Python (che può anche essere utilizzata per rappresentare matrici). Supporta un gran numero di array dimensionali e operazioni su matrici e fornisce anche un gran numero di librerie di funzioni matematiche per operazioni su array.

Le principali funzioni di NumPy:

  1. Ndarray, un oggetto di array multidimensionale, è una struttura dati veloce, flessibile e di risparmio di spazio.
  2. Operazioni di algebra lineare, inclusa la moltiplicazione di matrici, la trasposizione, l’inversione, ecc.
  3. Trasformata di Fourier, esegue una trasformata di Fourier veloce su un array.
  4. Operazione rapida di array in virgola mobile.
  5. Integrare il codice del linguaggio C in Python per eseguirlo più velocemente.

Utilizzando Aspose.Cells per Python via .NET API, è possibile convertire Excel, TSV, CSV, Json e molti formati diversi in ndarra NumPy.

Come convertire un Workbook di Excel in un ndarray NumPy

Ecco un esempio di snippet di codice per dimostrare come esportare i dati di Excel in un array NumPy utilizzando Aspose.Cells per Python via .NET:

  1. Caricare il file di esempio.
  2. Attraversare i dati di Excel ed esportare i dati in un ndarray NumPy utilizzando Aspose.Cells per 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)

Il risultato dell’output:

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

Come convertire un Foglio di lavoro in un ndarray NumPy

Ecco un esempio di snippet di codice per dimostrare come esportare i dati del foglio di lavoro in un ndarray NumPy utilizzando Aspose.Cells per Python via .NET:

  1. Caricare il file di esempio.
  2. Ottenere il primo foglio di lavoro.
  3. Convertire i dati del foglio di lavoro in un ndarray NumPy utilizzando la libreria Excel Aspose.Cells per 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)

Il risultato dell’output:

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

Come convertire un intervallo di Excel in un ndarray NumPy

Ecco un esempio di frammento di codice per dimostrare come esportare i dati dell’intervallo in un ndarray NumPy utilizzando Aspose.Cells per Python via .NET:

  1. Caricare il file di esempio.
  2. Ottenere il primo foglio di lavoro.
  3. Crea l’intervallo.
  4. Converti i dati dell’intervallo in un ndarray NumPy utilizzando la libreria Excel Aspose.Cells per 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)

Il risultato dell’output:

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

Come convertire un ListObject di Excel in un ndarray NumPy

Ecco un esempio di frammento di codice per dimostrare come esportare i dati dell’ListObject in un ndarray NumPy utilizzando Aspose.Cells per Python via .NET:

  1. Caricare il file di esempio.
  2. Ottenere il primo foglio di lavoro.
  3. Crea l’oggetto ListObject.
  4. Converti i dati dell’ListObject in un ndarray NumPy utilizzando la libreria Excel Aspose.Cells per 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
# 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)

Il risultato dell’output:

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

Come convertire una riga di Excel in un ndarray NumPy

Ecco un esempio di frammento di codice per dimostrare come esportare i dati della riga in un ndarray NumPy utilizzando Aspose.Cells per Python via .NET:

  1. Caricare il file di esempio.
  2. Ottenere il primo foglio di lavoro.
  3. Ottieni l’oggetto riga per indice riga.
  4. Convertire i dati della riga in un ndarray NumPy utilizzando la libreria Excel Aspose.Cells per 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
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)

Il risultato dell’output:

['Detroit' 'Central' '3074']

Come convertire una colonna di Excel in un ndarray NumPy.

Ecco un esempio di snippet di codice per dimostrare come esportare i dati della colonna in un ndarray NumPy utilizzando Aspose.Cells per Python via .NET:

  1. Caricare il file di esempio.
  2. Ottenere il primo foglio di lavoro.
  3. Ottenere l’oggetto colonna tramite l’indice della colonna.
  4. Convertire i dati della colonna in un ndarray NumPy utilizzando la libreria Excel Aspose.Cells per 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
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)

Il risultato dell’output:

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