我有一个元组列表列表,其中每个元组的长度相等,并且我需要将元组转换为 Pandas 数据帧,使得数据帧的列等于元组的长度,并且每个元组item 是跨列的行条目。
我咨询过有关此主题的其他问题(例如,将元组列表列表转换为 pandas 数据框, pandas 数据框的元组列表列表, 在元组列表的列表中拆分元组列表) 不成功。
我得到的最接近的是来自 Stack Overflow 上另一个问题的列表理解:
import pandas as pd
tupList = [[('commentID', 'commentText', 'date'), ('123456', 'blahblahblah', '2019')], [('45678', 'hello world', '2018'), ('0', 'text', '2017')]]
# Trying list comprehension from previous stack question:
pd.DataFrame([[y for y in x] for x in tupList])
但这会产生意想不到的结果:
0 1
0 (commentID, commentText, date) (123456, blahblahblah, 2019)
1 (45678, hello world, 2018) (0, text, 2017)
当预期结果如下:
0 1 2
0 commentID commentText date
1 123456 blahblahblah 2019
2 45678 hello world 2018
3 0 text 2017
总之:我需要等于每个元组长度的列(在示例中为 3),其中元组中的每个项目都是跨列的行条目。
Thanks!