<p>如果元组具有不同数量的元素,则更通用的解决方案是创建如下所示的自定义函数</p>
<pre><code>def create_columns_from_tuple(df, tuple_col):
# get max length of tuples
max_len = df[tuple_col].apply(lambda x: 0 if x is None else len(x)).max()
# select rows with non-empty tuples
df_full = df.loc[df[tuple_col].notna()]
# create dataframe with exploded tuples
df_full_exploded = pd.DataFrame(df_full[tuple_col].tolist(),
index=df_full.index,
columns=[tuple_col + str(n) for n in range(1, max_len+1)])
# merge the two dataframes by index
result = df.merge(df_full_exploded, left_index=True, right_index=True, how='left')
return result
</code></pre>
<p>在此函数中,传递数据帧和元组列的名称。该函数将自动创建与元组最大长度相同的列</p>
<pre><code>create_columns_from_tuple(df, tuple_col='b')
# a b b1 b2
# 0 NaN None NaN NaN
# 1 1.0 (1, 2) 1.0 2.0
# 2 2.0 (3, 4) 3.0 4.0
</code></pre>
<hr/>
<p>如果元组的元素数不同:</p>
<pre><code>df = pd.DataFrame({'a':[None,1, 2], 'b':[None, (1,2,42), (3,4)]})
create_columns_from_tuple(df, tuple_col='b')
# a b b1 b2 b3
# 0 NaN None NaN NaN NaN
# 1 1.0 (1, 2, 42) 1.0 2.0 42.0
# 2 2.0 (3, 4) 3.0 4.0 NaN
</code></pre>