我有一个包含嵌套元组的列表。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 作为第三列)。感觉就像一个简单的列表理解 - 但我却一片空白。任何帮助将不胜感激。
1 回答
![?](http://img1.sycdn.imooc.com/533e51f30001edf702000200-100-100.jpg)
波斯汪
TA贡献1811条经验 获得超4个赞
我们的确是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]
添加回答
举报
0/150
提交
取消