【发布时间】:2019-12-21 21:54:05
【问题描述】:
我有一个元组列表列表,其中每个元组的长度相同,我需要将元组转换为 Pandas 数据帧,使数据帧的列等于元组的长度,并且每个元组项都是跨列的行条目。
我已咨询过有关此主题的其他问题(例如,Convert a list of lists of tuples to pandas dataframe、List of list of tuples to pandas dataframe、split list of tuples in lists of list of tuples)未成功。
我得到的最接近的是 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),其中元组中的每个项目都是跨列的行条目。
谢谢!
【问题讨论】:
标签: python python-3.x pandas tuples list-comprehension