ExcelをPandas DataFrameに変換
Contents
[
Hide
]
Aspose.Cells for Python via .NET APIを使用すると、Excel、TSV、CSV、Jsonなど、さまざまな形式をpandas DataFrameに変換できます。
ExcelをJSONデータを介してPandas DataFrameに変換する
Aspose.Cells for Python via .NETを使用して、ExcelデータをJSONデータを介してPandas DataFrameにエクスポートする方法を示すコードスニペットの例です。
- Workbookを作成していくつかの値を追加します。
- ExcelデータをJSON文字列にエクスポートします。
- pandasライブラリを使用してJSONデータを読み込みます。
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, JsonSaveOptions | |
# Create a new Aspose.Cells Workbook | |
workbook = Workbook() | |
# Get the first worksheet | |
worksheet = workbook.worksheets[0] | |
# Get the cells | |
cells = worksheet.cells | |
# Add some values | |
cells.get("A1").value = "Name" | |
cells.get("B1").value = "Age" | |
cells.get("C1").value = "City" | |
cells.get("A2").value = "Alice" | |
cells.get("B2").value = 25 | |
cells.get("C2").value = "New York" | |
cells.get("A3").value = "Bob" | |
cells.get("B3").value = 30 | |
cells.get("C3").value = "San Francisco" | |
cells.get("A4").value = "Charlie" | |
cells.get("B4").value = 35 | |
cells.get("C4").value = "Los Angeles" | |
jsonSaveOptions = JsonSaveOptions() | |
# Save data to json string | |
json = JsonUtility.export_range_to_json(cells.max_display_range, jsonSaveOptions); | |
print(json) | |
# Read json string using pandas | |
dfData = pd.read_json(json) | |
print(dfData) |
Pandas DataFrame を直接 Excel に変換
Aspose.Cells for Python via .NETを使用して、ExcelデータをPandas DataFrameに直接エクスポートする方法を示すコードスニペットの例です。
- Workbookを作成していくつかの値を追加します。
- ExcelデータをトラバースしてAspose.Cells for Python via .NETを使用してPandas DataFrameにデータをエクスポートします。
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, JsonSaveOptions | |
# Create a new Aspose.Cells Workbook | |
workbook = Workbook() | |
# Get the first worksheet | |
worksheet = workbook.worksheets[0] | |
# Get the cells | |
cells = worksheet.cells | |
# Add some values | |
cells.get("A1").value = "Name" | |
cells.get("B1").value = "Age" | |
cells.get("C1").value = "City" | |
cells.get("A2").value = "Alice" | |
cells.get("B2").value = 25 | |
cells.get("C2").value = "New York" | |
cells.get("A3").value = "Bob" | |
cells.get("B3").value = 30 | |
cells.get("C3").value = "San Francisco" | |
cells.get("A4").value = "Charlie" | |
cells.get("B4").value = 35 | |
cells.get("C4").value = "Los Angeles" | |
rowCount = cells.max_data_row | |
columnCount = cells.max_data_column | |
columnDatas=[] | |
for c in range(columnCount + 1): | |
currCell = cells.get_cell(0, c) | |
columnDatas.append(currCell.value) | |
result = pd.DataFrame(columns=columnDatas, dtype=object) | |
for i in range(1, rowCount + 1): | |
rowarray = [] | |
for j in range(columnCount + 1): | |
currCell = cells.get_cell(i, j) | |
rowarray.append(currCell.value) | |
print(rowarray) | |
result.loc[i - 1] = rowarray | |
print(result) |