تحويل DataFrame لـ Pandas إلى Excel
Contents
[
Hide
]
باستخدام واجهة برمجة تطبيقات Aspose.Cells لـ Python via .NET ، يمكنك تحويل DataFrame لـ pandas إلى Excel ، OpenOffice ، Pdf ، Json والعديد من التنسيقات المختلفة.
تحويل DataFrame لـ Pandas إلى Excel عن طريق بيانات json
إليك مثال لتجريب الشفرة لتوضيح كيفية استيراد البيانات من إطار بيانات pandas إلى ملف Excel باستخدام Aspose.Cells لـ Python via .NET:
- إنشاء بيانات عينة لإطار البيانات باستخدام pandas.
- استخدام مكتبة pandas لتحويل بيانات إطار البيانات إلى بيانات JSON.
- استيراد بيانات JSON باستخدام Aspose.Cells لـ Python via .NET.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas as pd | |
from aspose.cells.utility import JsonUtility, JsonLayoutOptions | |
from aspose.cells import Workbook, Worksheet, Cells | |
# Create a sample pandas DataFrame | |
data = {'Name': ['Alice', 'Bob', 'Charlie'], | |
'Age': [25, 30, 35], | |
'City': ['New York', 'San Francisco', 'Los Angeles']} | |
df = pd.DataFrame(data) | |
# Convert pandas DataFrame to JSON | |
json_string = df.to_json(orient='records') | |
workbook = Workbook() | |
# Get the first worksheet | |
worksheet = workbook.worksheets[0] | |
# Get the cells | |
cells = worksheet.cells | |
options = JsonLayoutOptions() | |
unit = JsonUtility() | |
# Processes as table. | |
options.array_as_table = True | |
unit.import_data(json_string, cells, 0, 0, options) | |
workbook.save("out.xlsx") |
تحويل إطار بيانات Pandas إلى Excel مباشرة
إليك مثال لتجريب الشفرة لتوضيح كيفية استيراد البيانات من إطار بيانات pandas إلى ملف Excel باستخدام Aspose.Cells لـ Python via .NET:
- إنشاء بيانات عينة لإطار البيانات باستخدام pandas.
- عبور إطار البيانات واستيراد البيانات باستخدام Aspose.Cells لـ Python via .NET.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas as pd | |
import aspose.cells | |
from aspose.cells import Workbook, CellsHelper, License | |
workbook = Workbook() | |
# Get the first worksheet | |
worksheet = workbook.worksheets[0] | |
# Get the cells | |
cells = worksheet.cells | |
# create a sample DataFrame | |
data = {'name': ['Alice', 'Bob', 'Charlie', 'David'], | |
'age': [25, 32, 18, 47], | |
'city': ['New York', 'Paris', 'London', 'Berlin']} | |
df = pd.DataFrame(data) | |
rowindex = 0 | |
colindex = 0 | |
for column in df: | |
cell = cells.get(rowindex, colindex) | |
cell.put_value(df[column].name) | |
colindex += 1 | |
for index, row in df.iterrows(): | |
rowindex += 1 | |
colindex = 0 | |
cell = cells.get(rowindex, colindex) | |
cell.put_value(row["name"]) | |
colindex += 1 | |
cell = cells.get(rowindex, colindex) | |
cell.put_value(row["age"]) | |
colindex += 1 | |
cell = cells.get(rowindex, colindex) | |
cell.put_value(row["city"]) | |
colindex += 1 | |
workbook.save("out.xlsx") |