NumPy yı Excel e dönüştürün
NumPy’ya Giriş
NumPy (Sayısal Python), Python’un açık kaynaklı sayısal hesaplama uzantısıdır. Bu araç, büyük matrisleri depolamak ve işlemek için kullanılabilir ve Python’un iç içe liste yapısından (matrisleri temsil etmek için de kullanılabilir) çok daha verimlidir. Büyük sayıda boyutlu dizileri ve matris işlemlerini destekler ve ayrıca dizi işlemleri için geniş bir matematiksel işlev kütüphanesi sağlar.
NumPy’nın temel işlevleri:
- Ndarray, hızlı, esnek ve yer tasarrufu sağlayan çok boyutlu dizi nesnesidir.
- Matris çarpımı, transpozisyon, ters çevirme vb. içeren lineer cebir işlemleri.
- Fourier dönüşümü, bir dizide hızlı Fourier dönüşümü yapma.
- Kayan noktalı dizilerin hızlı işlemi.
- Python içine C dil kodunu entegre ederek daha hızlı çalışmasını sağlama.
Aspose.Cells for Python via .NET API’sını kullanarak NumPy ndarray’ını Excel, OpenOffice, Pdf, Json ve çok farklı formatlara dönüştürebilirsiniz.
NumPy’nı Excel Çalışma Kitabına Nasıl Dönüştürülür
Aspose.Cells for Python via .NET kullanarak bir NumPy dizisinden veri almayı gösteren bir örnek kod parçacığı:
- Örnek bir NumPy dizisi verisi oluşturun.
- NumPy dizisini dolaşın ve Aspose.Cells for Python via .NET kullanarak veri alın.
import numpy as np | |
import pandas as pd | |
import aspose.cells | |
from aspose.cells import Workbook, Worksheet, Range, CellsHelper | |
def put_cell_value(cells, raw_value, row , column): | |
cell = cells.get(row , column) | |
dtype = type(raw_value) | |
match dtype: | |
case np.bool_ : | |
value = bool(raw_value) | |
case np.int_ : | |
value = int(raw_value) | |
case np.intc : | |
value = int(raw_value) | |
case np.intp : | |
value = int(raw_value) | |
case np.int8 : | |
value = int(raw_value) | |
case np.int16 : | |
value = int(raw_value) | |
case np.int32 : | |
value = int(raw_value) | |
case np.int64 : | |
value = int(raw_value) | |
case np.uint8 : | |
value = int(raw_value) | |
case np.uint16 : | |
value = int(raw_value) | |
case np.uint32 : | |
value = int(raw_value) | |
case np.uint64 : | |
value = int(raw_value) | |
case np.float_: | |
value = int(raw_value) | |
case np.float16: | |
value = float(raw_value) | |
case np.float32: | |
value = float(raw_value) | |
case np.float64: | |
value = float(raw_value) | |
case np.single: | |
value = float(raw_value) | |
case np.double: | |
value = float(raw_value) | |
case np.datetime64 : | |
ts = pd.to_datetime(str(raw_value)) | |
value = ts.strftime('%Y.%m.%d') | |
case _: | |
value = raw_value | |
cell.put_value(value) | |
pass | |
def import_table_data_into_cells(cells,table_data,row_index,column_index,is_vertical): | |
table_row_index = row_index | |
table_column_index = column_index | |
row_count = len( table_data) | |
column_count = 0 | |
for table_row in table_data: | |
for table_cell in table_row: | |
column_count =len( table_row) | |
put_cell_value(cells,table_cell,table_row_index,table_column_index) | |
if is_vertical : | |
table_row_index = table_row_index + 1 | |
else: | |
table_column_index = table_column_index + 1 | |
if is_vertical : | |
table_row_index = row_index | |
table_column_index = table_column_index + 1 | |
else: | |
table_column_index = column_index | |
table_row_index = table_row_index + 1 | |
if is_vertical : | |
end_row_index = row_index + row_count - 1 | |
end_column_index = table_column_index - 1 | |
else: | |
end_row_index = table_row_index - 1 | |
end_column_index = column_index + column_count - 1 | |
return (row_index,column_index,end_row_index,end_column_index) | |
excel_data = np.array([[['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]]] ) | |
# Create a new Aspose.Cells Workbook | |
workbook = Workbook() | |
begin_row_index = 0 | |
begin_column_index = 0 | |
is_vertical = False | |
sheet_index = 1 | |
for table_data in excel_data: | |
curr_sheet = workbook.worksheets.add("Sheet_data_" + str(sheet_index)) | |
curr_cells = curr_sheet.cells | |
import_table_data_into_cells(curr_cells , table_data , begin_row_index,begin_column_index,is_vertical) | |
sheet_index += 1 | |
workbook.save("out.xlsx") |
Çıktı sonucu:
NumPy ndarray’yi Çalışma Sayfasına Nasıl Dönüştürülür
Aspose.Cells for Python via .NET kullanarak bir NumPy dizisinden veri almayı gösteren bir örnek kod parçacığı:
- Örnek bir NumPy dizisi verisi oluşturun.
- NumPy dizisini dolaşın ve Aspose.Cells for Python via .NET kullanarak veri alın.
import numpy as np | |
import pandas as pd | |
import aspose.cells | |
from aspose.cells import Workbook, Worksheet, Range, CellsHelper | |
def put_cell_value(cells, raw_value, row , column): | |
cell = cells.get(row , column) | |
dtype = type(raw_value) | |
match dtype: | |
case np.bool_ : | |
value = bool(raw_value) | |
case np.int_ : | |
value = int(raw_value) | |
case np.intc : | |
value = int(raw_value) | |
case np.intp : | |
value = int(raw_value) | |
case np.int8 : | |
value = int(raw_value) | |
case np.int16 : | |
value = int(raw_value) | |
case np.int32 : | |
value = int(raw_value) | |
case np.int64 : | |
value = int(raw_value) | |
case np.uint8 : | |
value = int(raw_value) | |
case np.uint16 : | |
value = int(raw_value) | |
case np.uint32 : | |
value = int(raw_value) | |
case np.uint64 : | |
value = int(raw_value) | |
case np.float_: | |
value = int(raw_value) | |
case np.float16: | |
value = float(raw_value) | |
case np.float32: | |
value = float(raw_value) | |
case np.float64: | |
value = float(raw_value) | |
case np.single: | |
value = float(raw_value) | |
case np.double: | |
value = float(raw_value) | |
case np.datetime64 : | |
ts = pd.to_datetime(str(raw_value)) | |
value = ts.strftime('%Y.%m.%d') | |
case _: | |
value = raw_value | |
cell.put_value(value) | |
pass | |
def import_table_data_into_cells(cells,table_data,row_index,column_index,is_vertical): | |
table_row_index = row_index | |
table_column_index = column_index | |
row_count = len( table_data) | |
column_count = 0 | |
for table_row in table_data: | |
for table_cell in table_row: | |
column_count =len( table_row) | |
put_cell_value(cells,table_cell,table_row_index,table_column_index) | |
if is_vertical : | |
table_row_index = table_row_index + 1 | |
else: | |
table_column_index = table_column_index + 1 | |
if is_vertical : | |
table_row_index = row_index | |
table_column_index = table_column_index + 1 | |
else: | |
table_column_index = column_index | |
table_row_index = table_row_index + 1 | |
if is_vertical : | |
end_row_index = row_index + row_count - 1 | |
end_column_index = table_column_index - 1 | |
else: | |
end_row_index = table_row_index - 1 | |
end_column_index = column_index + column_count - 1 | |
return (row_index,column_index,end_row_index,end_column_index) | |
# Create a sample NumPy array | |
data = np.array([['City', 'Region', 'Store'], ['Chicago', 'Central', 3055], ['New York', 'East', 3036], ['Detroit', 'Central', 3074]]) | |
# Create a new Aspose.Cells Workbook | |
workbook = Workbook() | |
# Access the first (default) worksheet | |
worksheet = workbook.worksheets[0] | |
# Get the cells | |
cells = worksheet.cells | |
begin_row_index = 0 | |
begin_column_index = 0 | |
is_vertical = False | |
import_table_data_into_cells(cells , data , begin_row_index,begin_column_index,is_vertical) | |
workbook.save("out.xlsx") |
Çıktı sonucu:
NumPy ndarray’yi ListObject’e Nasıl Dönüştürülür
Aspose.Cells for Python via .NET kullanarak bir NumPy dizisinden veri almayı gösteren bir örnek kod parçacığı:
- Örnek bir NumPy dizisi verisi oluşturun.
- NumPy dizisini dolaşın ve Aspose.Cells for Python via .NET kullanarak veri alın. Örnek veri kullanarak ListObject nesnesi oluşturun.
import numpy as np | |
import pandas as pd | |
import aspose.cells | |
from aspose.cells import Workbook, Worksheet, Range, CellsHelper | |
def put_cell_value(cells, raw_value, row , column): | |
cell = cells.get(row , column) | |
dtype = type(raw_value) | |
match dtype: | |
case np.bool_ : | |
value = bool(raw_value) | |
case np.int_ : | |
value = int(raw_value) | |
case np.intc : | |
value = int(raw_value) | |
case np.intp : | |
value = int(raw_value) | |
case np.int8 : | |
value = int(raw_value) | |
case np.int16 : | |
value = int(raw_value) | |
case np.int32 : | |
value = int(raw_value) | |
case np.int64 : | |
value = int(raw_value) | |
case np.uint8 : | |
value = int(raw_value) | |
case np.uint16 : | |
value = int(raw_value) | |
case np.uint32 : | |
value = int(raw_value) | |
case np.uint64 : | |
value = int(raw_value) | |
case np.float_: | |
value = int(raw_value) | |
case np.float16: | |
value = float(raw_value) | |
case np.float32: | |
value = float(raw_value) | |
case np.float64: | |
value = float(raw_value) | |
case np.single: | |
value = float(raw_value) | |
case np.double: | |
value = float(raw_value) | |
case np.datetime64 : | |
ts = pd.to_datetime(str(raw_value)) | |
value = ts.strftime('%Y.%m.%d') | |
case _: | |
value = raw_value | |
cell.put_value(value) | |
pass | |
def import_table_data_into_cells(cells,table_data,row_index,column_index,is_vertical): | |
table_row_index = row_index | |
table_column_index = column_index | |
row_count = len( table_data) | |
column_count = 0 | |
for table_row in table_data: | |
for table_cell in table_row: | |
column_count =len( table_row) | |
put_cell_value(cells,table_cell,table_row_index,table_column_index) | |
if is_vertical : | |
table_row_index = table_row_index + 1 | |
else: | |
table_column_index = table_column_index + 1 | |
if is_vertical : | |
table_row_index = row_index | |
table_column_index = table_column_index + 1 | |
else: | |
table_column_index = column_index | |
table_row_index = table_row_index + 1 | |
if is_vertical : | |
end_row_index = row_index + row_count - 1 | |
end_column_index = table_column_index - 1 | |
else: | |
end_row_index = table_row_index - 1 | |
end_column_index = column_index + column_count - 1 | |
return (row_index,column_index,end_row_index,end_column_index) | |
# Create a sample NumPy array | |
data = np.array([['City', 'Region', 'Store'], ['Chicago', 'Central', 3055], ['New York', 'East', 3036], ['Detroit', 'Central', 3074]]) | |
# Create a new Aspose.Cells Workbook | |
workbook = Workbook() | |
# Access the first (default) worksheet | |
worksheet = workbook.worksheets[0] | |
# Get the cells | |
cells = worksheet.cells | |
begin_row_index = 0 | |
begin_column_index = 0 | |
is_vertical = False | |
# ndarray to listobject | |
(begin_row_index,begin_column_index,end_row_index,end_column_index) = import_table_data_into_cells(cells , data , begin_row_index,begin_column_index,is_vertical) | |
start_cell = CellsHelper.cell_index_to_name(begin_row_index, begin_column_index) | |
end_cell = CellsHelper.cell_index_to_name(end_row_index, end_column_index) | |
index = worksheet.list_objects.add(start_cell, end_cell,True) | |
workbook.save("out.xlsx") |
Çıktı sonucu:
NumPy ndarray’yi Aralık’a Nasıl Dönüştürülür
Aspose.Cells for Python via .NET kullanarak bir NumPy dizisinden veri almayı gösteren bir örnek kod parçacığı:
- Örnek bir NumPy dizisi verisi oluşturun.
- NumPy dizisini dolaşın ve Aspose.Cells for Python via .NET kullanarak veri alın. Örnek veri kullanarak Range nesnesi oluşturun.
import numpy as np | |
import pandas as pd | |
import aspose.cells | |
from aspose.cells import Workbook, Worksheet, Range, CellsHelper | |
def put_cell_value(cells, raw_value, row , column): | |
cell = cells.get(row , column) | |
dtype = type(raw_value) | |
match dtype: | |
case np.bool_ : | |
value = bool(raw_value) | |
case np.int_ : | |
value = int(raw_value) | |
case np.intc : | |
value = int(raw_value) | |
case np.intp : | |
value = int(raw_value) | |
case np.int8 : | |
value = int(raw_value) | |
case np.int16 : | |
value = int(raw_value) | |
case np.int32 : | |
value = int(raw_value) | |
case np.int64 : | |
value = int(raw_value) | |
case np.uint8 : | |
value = int(raw_value) | |
case np.uint16 : | |
value = int(raw_value) | |
case np.uint32 : | |
value = int(raw_value) | |
case np.uint64 : | |
value = int(raw_value) | |
case np.float_: | |
value = int(raw_value) | |
case np.float16: | |
value = float(raw_value) | |
case np.float32: | |
value = float(raw_value) | |
case np.float64: | |
value = float(raw_value) | |
case np.single: | |
value = float(raw_value) | |
case np.double: | |
value = float(raw_value) | |
case np.datetime64 : | |
ts = pd.to_datetime(str(raw_value)) | |
value = ts.strftime('%Y.%m.%d') | |
case _: | |
value = raw_value | |
cell.put_value(value) | |
pass | |
def import_table_data_into_cells(cells,table_data,row_index,column_index,is_vertical): | |
table_row_index = row_index | |
table_column_index = column_index | |
row_count = len( table_data) | |
column_count = 0 | |
for table_row in table_data: | |
for table_cell in table_row: | |
column_count =len( table_row) | |
put_cell_value(cells,table_cell,table_row_index,table_column_index) | |
if is_vertical : | |
table_row_index = table_row_index + 1 | |
else: | |
table_column_index = table_column_index + 1 | |
if is_vertical : | |
table_row_index = row_index | |
table_column_index = table_column_index + 1 | |
else: | |
table_column_index = column_index | |
table_row_index = table_row_index + 1 | |
if is_vertical : | |
end_row_index = row_index + row_count - 1 | |
end_column_index = table_column_index - 1 | |
else: | |
end_row_index = table_row_index - 1 | |
end_column_index = column_index + column_count - 1 | |
return (row_index,column_index,end_row_index,end_column_index) | |
# Create a sample NumPy array | |
data = np.array([['City', 'Region', 'Store'], ['Chicago', 'Central', 3055], ['New York', 'East', 3036], ['Detroit', 'Central', 3074]]) | |
# Create a new Aspose.Cells Workbook | |
workbook = Workbook() | |
# Access the first (default) worksheet | |
worksheet = workbook.worksheets[0] | |
# Get the cells | |
cells = worksheet.cells | |
begin_row_index = 0 | |
begin_column_index = 0 | |
is_vertical = False | |
# ndarray to range | |
(begin_row_index,begin_column_index,end_row_index,end_column_index) = import_table_data_into_cells(cells , data , begin_row_index,begin_column_index,is_vertical) | |
start_cell = CellsHelper.cell_index_to_name(begin_row_index, begin_column_index) | |
end_cell = CellsHelper.cell_index_to_name(end_row_index, end_column_index) | |
range_obj = cells.create_range(start_cell, end_cell) | |
print("row count: " + str(range_obj.row_count)) | |
print("column count: " + str(range_obj.column_count)) | |
workbook.save("out.xlsx") |
Çıktı sonucu:
row count: 4
column count: 3
NumPy ndarray’yi İsim’e Nasıl Dönüştürülür
Aspose.Cells for Python via .NET kullanarak bir NumPy dizisinden veri almayı gösteren bir örnek kod parçacığı:
- Örnek bir NumPy dizisi verisi oluşturun.
- NumPy dizisini dolaşın ve Aspose.Cells for Python via .NET kullanarak veri alın. Örnek veri kullanarak Name nesnesi oluşturun.
import numpy as np | |
import pandas as pd | |
import aspose.cells | |
from aspose.cells import Workbook, Worksheet, Range, CellsHelper | |
def put_cell_value(cells, raw_value, row , column): | |
cell = cells.get(row , column) | |
dtype = type(raw_value) | |
match dtype: | |
case np.bool_ : | |
value = bool(raw_value) | |
case np.int_ : | |
value = int(raw_value) | |
case np.intc : | |
value = int(raw_value) | |
case np.intp : | |
value = int(raw_value) | |
case np.int8 : | |
value = int(raw_value) | |
case np.int16 : | |
value = int(raw_value) | |
case np.int32 : | |
value = int(raw_value) | |
case np.int64 : | |
value = int(raw_value) | |
case np.uint8 : | |
value = int(raw_value) | |
case np.uint16 : | |
value = int(raw_value) | |
case np.uint32 : | |
value = int(raw_value) | |
case np.uint64 : | |
value = int(raw_value) | |
case np.float_: | |
value = int(raw_value) | |
case np.float16: | |
value = float(raw_value) | |
case np.float32: | |
value = float(raw_value) | |
case np.float64: | |
value = float(raw_value) | |
case np.single: | |
value = float(raw_value) | |
case np.double: | |
value = float(raw_value) | |
case np.datetime64 : | |
ts = pd.to_datetime(str(raw_value)) | |
value = ts.strftime('%Y.%m.%d') | |
case _: | |
value = raw_value | |
cell.put_value(value) | |
pass | |
def import_table_data_into_cells(cells,table_data,row_index,column_index,is_vertical): | |
table_row_index = row_index | |
table_column_index = column_index | |
row_count = len( table_data) | |
column_count = 0 | |
for table_row in table_data: | |
for table_cell in table_row: | |
column_count =len( table_row) | |
put_cell_value(cells,table_cell,table_row_index,table_column_index) | |
if is_vertical : | |
table_row_index = table_row_index + 1 | |
else: | |
table_column_index = table_column_index + 1 | |
if is_vertical : | |
table_row_index = row_index | |
table_column_index = table_column_index + 1 | |
else: | |
table_column_index = column_index | |
table_row_index = table_row_index + 1 | |
if is_vertical : | |
end_row_index = row_index + row_count - 1 | |
end_column_index = table_column_index - 1 | |
else: | |
end_row_index = table_row_index - 1 | |
end_column_index = column_index + column_count - 1 | |
return (row_index,column_index,end_row_index,end_column_index) | |
# Create a sample NumPy array | |
data = np.array([['City', 'Region', 'Store'], ['Chicago', 'Central', 3055], ['New York', 'East', 3036], ['Detroit', 'Central', 3074]]) | |
# Create a new Aspose.Cells Workbook | |
workbook = Workbook() | |
# Access the first (default) worksheet | |
worksheet = workbook.worksheets[0] | |
# Get the cells | |
cells = worksheet.cells | |
begin_row_index = 0 | |
begin_column_index = 0 | |
is_vertical = False | |
# ndarray to Name | |
(begin_row_index,begin_column_index,end_row_index,end_column_index) = import_table_data_into_cells(cells , data , begin_row_index,begin_column_index,is_vertical) | |
start_cell = CellsHelper.cell_index_to_name(begin_row_index, begin_column_index) | |
end_cell = CellsHelper.cell_index_to_name(end_row_index, end_column_index) | |
name_text = "Name_test" | |
name_refers_to = "=" + worksheet.name + "!$" + start_cell + ":$" + end_cell | |
name_index = worksheet.workbook.worksheets.names.add(name_text); | |
name = worksheet.workbook.worksheets.names[name_index] | |
name.refers_to = name_refers_to | |
workbook.save("out.xlsx") |
Çıktı sonucu: