You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何拆分Pandas DataFrame中的双层嵌套列?解决索引越界报错

Solution to Nested Column Extraction and Index Error

First, let's fix the "list index out of range" error you're encountering. The issue arises because some rows in your type column have empty lists—your lambda assumes every entry has at least one element at index 0, which isn't always true.

Fixing the Details Extraction Error

Modify your lambda to check if the list is non-empty before accessing its first element:

df['details'] = df['type'].apply(lambda x: x[0]['details'] if len(x) > 0 else None)

This returns None (converted to NaN in pandas) for rows with empty type lists, preventing the index error.

Handling the Nested Cause Field

Manual lambda extraction gets messy for deeply nested structures like Cause. Instead, use pandas' pd.json_normalize—it's designed to flatten nested JSON/dict structures into clean DataFrame columns.

Here's a step-by-step approach:

  1. Convert type lists to dicts:
    Since each type entry is a list (with 0 or 1 elements), convert it to a dict (empty dict if the list is empty):

    type_dicts = df['type'].apply(lambda x: x[0] if len(x) > 0 else {})
    
  2. Flatten the nested structure:
    Use json_normalize to expand both the top-level fields in type and the nested Cause fields:

    type_expanded = pd.json_normalize(type_dicts)
    

    This will create columns like details, id, machine, Cause.code, Cause.description, Cause.id, and Cause.reason.

  3. Merge with original DataFrame:
    Combine the expanded type data with your original DataFrame, dropping the original type column:

    final_df = pd.concat([df.drop('type', axis=1), type_expanded], axis=1)
    

Full Example with Your Sample Data

If we apply this to your sample entry, the final_df will have these columns:
Speed, endTime, line, level, lineId, loss, startTime, details, id, machine, Cause.code, Cause.description, Cause.id, Cause.reason

This method is robust, handles missing fields automatically (filling with NaN), and scales well if your nested structure has more fields later on.

内容的提问来源于stack exchange,提问作者Keithx

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 09:25:32