Pandas 数据帧的元组列表列表? [英] List of LISTS of tuples to Pandas dataframe?
问题描述
我有一个元组列表的列表,其中每个元组的长度相等,我需要将元组转换为Pandas数据框,使数据框的列等于元组的长度,每个元组项目都是跨列的行条目.
I have a list of lists of tuples, where every tuple is of equal length, and I need to convert the tuples to a Pandas dataframe in such a way that the columns of the dataframe are equal to the length of the tuples, and each tuple item is a row entry across the columns.
我已经咨询了有关此主题的其他问题(例如,熊猫数据框的元组列表,
I have consulted other questions on this topic (e.g., 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) unsuccessfully.
我最接近的是列表理解,它来自Stack Overflow上的另一个问题:
The closest I get is with list comprehension from a different question on 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])
但这会产生意想不到的结果:
But this yields the unintended result:
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),其中元组中的每个项目都是跨列的行条目.
In sum: I need columns equal to the length of each tuple (in the example, 3), where each item within the tuple is a row entry across the columns.
谢谢!
推荐答案
只需将列表展平到元组列表中(您的初始列表包含元组的子列表):
Just flatten your list into a list of tuples (your initial list contains a sublists of tuples):
In [1251]: tupList = [[('commentID', 'commentText', 'date'), ('123456', 'blahblahblah', '2019')], [('45678', 'hello world', '2018'), ('0', 'text', '2017')]]
In [1252]: pd.DataFrame([t for lst in tupList for t in lst])
Out[1252]:
0 1 2
0 commentID commentText date
1 123456 blahblahblah 2019
2 45678 hello world 2018
3 0 text 2017
这篇关于Pandas 数据帧的元组列表列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!