تحويل إكسل إلى 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

إليك مثال على مقتطفات الكود لتوضيح كيفية تصدير بيانات Excel إلى مصفوفة NumPy باستخدام Aspose.Cells for Python via .NET:

  1. قم بتحميل ملف العينة.
  2. قم بتصفح بيانات Excel وتصدير البيانات إلى NumPy ndarray باستخدام Aspose.Cells for 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)

نتيجة الإخراج:

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

إليك مثال على مقتطفات الكود لتوضيح كيفية تصدير بيانات ورق العمل إلى Numpy ndarray باستخدام Aspose.Cells for Python via .NET:

  1. قم بتحميل ملف العينة.
  2. احصل على ورقة العمل الأولى.
  3. تحويل بيانات ورق العمل إلى Numpy ndarray باستخدام مكتبة Aspose.Cells for 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
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

إليك مثال على مقتطفات الكود لتوضيح كيفية تصدير بيانات المجموعة إلى Numpy ndarray باستخدام Aspose.Cells for Python via .NET:

  1. قم بتحميل ملف العينة.
  2. احصل على ورقة العمل الأولى.
  3. إنشاء المجموعة.
  4. تحويل بيانات المجموعة إلى Numpy ndarray باستخدام مكتبة Aspose.Cells for 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)
# 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']]

كيفية تحويل ListObject من Excel إلى NumPy ndarray

إليك مقتطف كود مثالي يوضح كيفية تصدير بيانات ListObject إلى NumPy ndarray باستخدام Aspose.Cells for Python via .NET

  1. قم بتحميل ملف العينة.
  2. احصل على ورقة العمل الأولى.
  3. أنشئ كائن ListObject.
  4. قم بتحويل بيانات ListObject إلى NumPy ndarray باستخدام مكتبة Aspose.Cells for 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)

نتيجة الإخراج:

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

كيفية تحويل صف Excel إلى NumPy ndarray

إليك مقتطف كود مثالي يوضح كيفية تصدير بيانات الصف إلى NumPy ndarray باستخدام Aspose.Cells for Python via .NET

  1. قم بتحميل ملف العينة.
  2. احصل على ورقة العمل الأولى.
  3. احصل على كائن الصف بواسطة فهرس الصف.
  4. قم بتحويل بيانات الصف إلى NumPy ndarray باستخدام مكتبة Aspose.Cells for 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)

نتيجة الإخراج:

['Detroit' 'Central' '3074']

كيفية تحويل عمود Excel إلى NumPy ndarray

إليك مقتطف كود مثالي يوضح كيفية تصدير بيانات العمود إلى NumPy ndarray باستخدام Aspose.Cells for Python via .NET

  1. قم بتحميل ملف العينة.
  2. احصل على ورقة العمل الأولى.
  3. احصل على كائن العمود بواسطة فهرس العمود.
  4. قم بتحويل بيانات العمود إلى NumPy ndarray باستخدام مكتبة Aspose.Cells for 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)

نتيجة الإخراج:

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