Excel处理控件Aspose.Cells教程:在 Python 中将 JSON 转换为 Pandas DataFrame
<p><img src="https://image.evget.com/attachment/keditor/image/20250901/135136_0.png"></p><p>在数据分析、报告和 ETL 流程中,使用 JSON 和 Pandas DataFrame 非常常见。虽然 Pandas 提供了read_json基本的解析功能,但它在处理深度嵌套结构、超大文件或 Excel 优先工作流时可能会遇到困难。<strong><u>Aspose.Cells for Python</u></strong> 提供了丰富的 JSON 到 Excel 转换流程,您可以轻松地将其与 Pandas 集成,以获取干净的 DataFrame 进行分析。在本篇教程中,您将学习如何在 Python 中将 JSON 转换为 Pandas DataFrame。</p>
<p style="text-align: center"><span style="color: rgba(230, 126, 35, 1)"><strong>Aspose.Cells官方试用版免费下载,请联系Aspose官方授权代理商慧都科技</strong></span></p>
<p style="text-align: center"><span style="color: rgba(230, 126, 35, 1)"><strong><em>加入Aspose技术交流QQ群(1041253375),与更多小伙伴一起探讨提升开发技能。</em></strong></span></p>
<h2 id="python-library-to-convert-json-to-pandas-dataframe">将 JSON 转换为 Pandas DataFrame 的 Python 库</h2>
<p><strong><u>Aspose.Cells for Python via .NET</u></strong>是一款功能强大的电子表格 API,无需 Microsoft Excel。除了传统的 Excel 自动化功能外,它还支持直接导入和导出 JSON,非常适合将 JSON 转换为 Pandas DataFrame,然后在 Excel 中保存或处理。</p>
<p>使用 <strong><u>Aspose.Cells</u></strong>,您可以:</p>
<ul>
<li>使用<strong>将 JSON 导入工作表</strong>JsonUtility,并提供处理数组和嵌套结构的选项。</li>
<li><strong>将工作表范围转换为 Pandas DataFrames</strong>以进行分析和可视化。</li>
<li><strong>在 Excel 文件内创建、加载和处理 JSON</strong>,以适合分析管道。</li>
<li><strong>将 DataFrames 导出回 Excel</strong>(XLSX、CSV、ODS、PDF)以进行报告。</li>
</ul>
<p>简而言之,该库可以轻松地将数据从 JSON 格式迁移到 Excel 中用于报告,同时您可以使用 Pandas 进行更深入的分析。它将JsonUtilityJSON 导入工作表,并JsonLayoutOptions控制数组和嵌套对象的扩展方式。</p>
<h2 id="convert-json-to-dataframe">将 JSON 转换为 DataFrame</h2>
<p><strong><u>Aspose.Cells</u></strong> 直接将 JSON 导入工作表。然后读取标题行和数据行,构建 Pandas DataFrame。</p>
<p>按照以下步骤将 JSON 转换为 pandas DataFrame:</p>
<ol>
<li>创建一个工作簿并获取第一个工作表。</li>
<li>配置JsonLayoutOptions将数组视为表。</li>
<li>0在行、列处导入 JSON 字符串0。</li>
<li>使用第一行作为列标题。</li>
<li>提取剩余的行作为数据。</li>
<li>构建一个 Pandas DataFrame。</li>
</ol>
<p>以下代码示例展示了如何在 Python 中将 JSON 转换为 pandas DataFrame:</p>
<pre class="prettyprint lang-js highlighter-hljs"><code>import pandas as pd
import aspose.cells as ac
# Create a new workbook and get the first worksheet (0-based index)
wb = ac.Workbook()
ws = wb.worksheets.get(0)
# Configure how JSON should be laid out in the worksheet
options = ac.utility.JsonLayoutOptions()
options.array_as_table = True # Treat a top-level JSON array as a table (rows/columns)
# Example JSON array of simple objects
json_data = '[{"id":1,"name":"Alice"},{"id":2,"name":"Bob"}]'
# Import JSON into the worksheet starting at row=0, col=0 (cell A1)
ac.utility.JsonUtility.import_data(json_data, ws.cells, 0, 0, options)
# Locate the first row that contains data (this will be our header row)
header_idx = ws.cells.min_data_row
# Extract header values from that row to use as DataFrame column names
columns = ]
# Extract all subsequent rows as data (skip the header row)
data = [
for idx, row in enumerate(ws.cells.rows)
if row and idx != header_idx
]
# Build the DataFrame using the collected headers and rows
df = pd.DataFrame(data, columns=columns)
# Display the result
print(df)</code></pre>
<p><strong>输出:</strong></p>
<pre class="prettyprint lang-py highlighter-hljs"><code> id name
01.0Alice
12.0 Bob</code></pre>
<h2 id="convert-nested-json-to-pandas-dataframe">将嵌套 JSON 转换为 Pandas DataFrame</h2>
<p>如果您的 JSON 包含嵌套对象,<strong><u>Aspose.Cells</u></strong> 会使用 JsonUtility 将 JSON 导入工作表,然后您可以将其导出到 DataFrame。JsonLayoutOptions 控制数组和嵌套对象的展开方式。</p>
<p>按照以下步骤将嵌套 JSON 转换为 pandas DataFrame:</p>
<ol>
<li>创建一个工作簿并选择第一个工作表。</li>
<li>设置所需的JsonLayoutOptions属性,例如array_as_table=True、ignore_array_title=True、ignore_object_title=True和kept_schema=True。</li>
<li>在行0、列处导入嵌套的 JSON 0。</li>
<li>检测使用的范围并读取整个跨度内的标题行。</li>
<li>读取同一跨度(固定宽度)的所有后续行。</li>
<li>构建 DataFrame;可选地转换数据类型(例如,total转换为数字)。</li>
</ol>
<p>以下代码示例展示了如何在 Python 中将嵌套 JSON 转换为 pandas DataFrame:</p>
<pre class="prettyprint lang-js highlighter-hljs"><code>import pandas as pd
import aspose.cells as ac
# Create Workbook and get first worksheet
wb = ac.Workbook()
ws = wb.worksheets.get(0)
# Layout options for nested JSON
opt = ac.utility.JsonLayoutOptions()
opt.array_as_table = True# Treat 'orders' array as a table (rows)
opt.ignore_array_title = True# Do not place a title row for the 'orders' array
opt.ignore_object_title = True# Do not place extra title rows for nested objects (e.g., 'buyer')
opt.kept_schema = True# Keep a stable set of columns even if some records miss fields
# Step 3: Your nested JSON
nested = '''
{
"batch": "A1",
"orders": [
{"orderId": "1001", "total": "49.90", "buyer": {"city": "NYC", "zip": "10001"}},
{"orderId": "1002", "total": "79.00", "buyer": {"city": "Boston", "zip": "02108"}}
]
}
'''
# Import at A1 (row=0, col=0) using the options above
ac.utility.JsonUtility.import_data(nested, ws.cells, 0, 0, opt)
# Detect used range
first_row = ws.cells.min_data_row
first_col = ws.cells.min_data_column
last_row = ws.cells.max_data_row
last_col = ws.cells.max_data_column
# Read header row across the full used column span (fixed width)
raw_columns =
# Make headers safe: replace None/blank with "Column{n}" and cast to str
columns = [
(str(v) if v is not None and str(v).strip() != "" else f"Column{idx + 1}")
for idx, v in enumerate(raw_columns)
]
# Read data rows across the same span (fixed width guarantees alignment)
data = []
for r in range(first_row + 1, last_row + 1):
row_vals =
data.append(row_vals)
# Build DataFrame
df = pd.DataFrame(data, columns=columns)
# Optional: tidy up column names (e.g., replace spaces)
df.columns =
# Optional typing:
# - Keep ZIPs as strings (leading zeros matter)
# - Convert totals to numeric (coerce non-numeric to NaN)
for col in list(df.columns):
if col.lower().endswith("total"):
df = pd.to_numeric(df, errors="coerce")
print(df)</code></pre>
<p>输出:</p>
<pre class="prettyprint lang-py highlighter-hljs"><code> A1100149.90 NYC10001
0None100279.00Boston02108</code></pre>
<blockquote>
<p><em>注意:</em>如果启用convert_numeric_or_date=True,看起来像数字的字符串(例如总数)可能会转换为数字,但邮政编码(例如)"02108"可能会丢失前导零。False如果您需要将邮政编码转换为字符串,请保留此选项。</p>
</blockquote>
<h2 id="excel-to-pandas-via-json">通过 JSON 将 Excel 转换为 Pandas DataFrame</h2>
<p>使用 <strong><u>Aspose.Cells</u></strong> 将任意 Excel 范围导出为 JSON,然后将该 JSON 作为 DataFrame 加载到 Pandas 中。当您需要为服务或管道进行结构化 JSON 交接时,此功能非常有用。</p>
<p>按照以下步骤通过 JSON 将 Excel 转换为 pandas DataFrame:</p>
<ol>
<li>创建一个新工作簿,获取第一个工作表,并添加示例值。</li>
<li>使用默认值创建JsonSaveOptions。</li>
<li>使用该方法将使用的范围导出为JSON字符串export_range_to_json()。</li>
<li>使用该方法将 JSON 字符串读入 DataFrame pd.read_json(io.StringIO(json_text))。</li>
<li>根据需要检查或处理 DataFrame。</li>
</ol>
<p>以下代码示例展示了如何在 Python 中通过 JSON 将 Excel 转换为 pandas DataFrame:</p>
<pre class="prettyprint lang-js highlighter-hljs"><code>import io
import pandas as pd
from aspose.cells.utility import JsonUtility# JSON export utility
from aspose.cells import Workbook, JsonSaveOptions, License
# Create a new workbook and access the first worksheet
workbook = Workbook()
worksheet = workbook.worksheets.get(0)
# Get the cells of the worksheet
cells = worksheet.cells
# Populate a small table (headers + rows)
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"
# Set up JSON save options (defaults are fine for a simple table)
json_save_options = JsonSaveOptions()
# Export the used range to a JSON string
# max_display_range grabs the full rectangular region that contains data
json_text = JsonUtility.export_range_to_json(cells.max_display_range, json_save_options)
# Read the JSON string into a Pandas DataFrame
# Pandas can parse a JSON string directly
df = pd.read_json(io.StringIO(json_text))
# Use the DataFrame
print(df)</code></pre>
<p>输出:</p>
<pre class="prettyprint lang-py highlighter-hljs"><code> NameAge City
0 Alice 25 New York
1 Bob 30San Francisco
2Charlie 35 Los Angeles</code></pre>
<h2 id="conclusion">结论</h2>
<p>使用<strong><u> Aspose.Cells for Python</u></strong>,将 JSON 转换为 Pandas DataFrames 变得非常简单。您可以获得可靠的嵌套结构处理、模式稳定性选项,以及在需要时轻松导出到 Excel 的途径。将 Pandas 的灵活性与 <strong><u>Aspose.Cells</u></strong> 中的 JSON/Excel 管道相结合,简化数据处理并解锁强大的 Python 分析功能。</p>
<p style="text-align: center"><span style="color: rgba(230, 126, 35, 1)"><strong>Aspose.Cells官方试用版免费下载,请联系Aspose官方授权代理商慧都科技</strong></span></p>
<p style="text-align: center"><span style="color: rgba(230, 126, 35, 1)"><strong><em>加入Aspose技术交流QQ群(1041253375),与更多小伙伴一起探讨提升开发技能。</em></strong></span></p><br><br>
来源:https://www.cnblogs.com/software-Development/p/19068446
頁:
[1]