我有一个嵌套元组的列表。
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')]]]
我想将列表放平成熊猫数据框。
df = pd.DataFrame(nested, columns =('id', 'This column of tuples needs to split into two'))
df
结果是,
但不会将元组分成两列,每行一个元组(关联的ID为第三列)。感觉像是一个简单的列表理解功能,但我还是空白。任何帮助将不胜感激。
我们的确是 explode
s = 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()))
s
Out[162]:
c1 ... 1
0 53062423-690f-4923-8f65-db710c038566 ... AFC_PoCv1.0
1 53062423-690f-4923-8f65-db710c038566 ... Customer Profitability Sample
2 53062423-690f-4923-8f65-db710c038566 ... Jammers vs Floaty Pants
3 988f64ea-14b2-4ad7-a899-ae40974c9139 ... DailySalesDashboard13Azure
[4 rows x 4 columns]
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句