猿问

从列表中提取嵌套元组(Python)

我有一个包含嵌套元组的列表。

nested = [['53062423-690f-4923-8f65-db710c038566', [('12253996-b2f7-46c7-b49f-09ca87cac84f', 'AFC_PoCv1.0'), ('b17bd025-611f-4728-9396-e59388ee59f6', 'Customer Profitability Sample'), ('b4a5d199-2c6f-4f8d-9fcb-5e4971254f73', 'Jammers vs Floaty Pants')]], ['988f64ea-14b2-4ad7-a899-ae40974c9139', [('9137e0f9-4063-479a-baff-8566c91302ff', 'DailySalesDashboard13Azure')]]]

我想将列表展平为 pandas 数据框。

df = pd.DataFrame(nested, columns =('id', 'This column of tuples needs to split into two'))
df

结果是,

但不会将元组拆分为两列,每行一个元组(关联的 id 作为第三列)。感觉就像一个简单的列表理解 - 但我却一片空白。任何帮助将不胜感激。



德玛西亚99
浏览 155回答 1
1回答

波斯汪

我们的确是explodes = pd.DataFrame(nested,columns=['c1','c2']).explode('c2').reset_index(drop=True)# if only need to split the tuple , you do not need to do the next steps 将元组拆分为单列s = s.join(pd.DataFrame(s['c2'].tolist()))sOut[162]:                                      c1  ...                              10  53062423-690f-4923-8f65-db710c038566  ...                    AFC_PoCv1.01  53062423-690f-4923-8f65-db710c038566  ...  Customer Profitability Sample2  53062423-690f-4923-8f65-db710c038566  ...        Jammers vs Floaty Pants3  988f64ea-14b2-4ad7-a899-ae40974c9139  ...     DailySalesDashboard13Azure[4 rows x 4 columns]
随时随地看视频慕课网APP

相关分类

Python
我要回答