简体   繁体   English

列表列表 - 分隔元组以仅使用 pandas dataframe 中的特定列表

[英]List of Lists - Separating tuple to use only specific lists in pandas dataframe

I have a function that returns a list of lists (see image below).我有一个返回列表列表的 function(见下图)。 The first list is an identification number.第一个列表是标识号。 The remaining lists identify an item and then values related to the item.其余列表标识一个项目,然后标识与该项目相关的值。 What I'm trying to do is take all lists other than list[0] and place them in a pandas dataframe.我要做的是获取除 list[0] 以外的所有列表并将它们放在 pandas dataframe 中。 I know how to take an entire list of lists and create a df.我知道如何获取整个列表列表并创建一个 df。

data = lists
df = pd.DataFrame(data)

Can anyone help me make a dataframe minus the first list?谁能帮我制作一个 dataframe 减去第一个列表? If you have a suggestion to make this question easier to understand, or a link to where this is already solved, I'd appreciate the help.如果您有让这个问题更容易理解的建议,或者有一个指向已经解决的地方的链接,我将不胜感激。 I searched stack overflow but couldn't find a question on point.我搜索了堆栈溢出,但找不到正确的问题。 If this is just a dumb idea to do it this way for some reason, I'm new and it'd be helpful to point me in a better direction as well.如果出于某种原因这样做只是一个愚蠢的想法,那么我是新手,给我指出一个更好的方向也会很有帮助。 But some of the lists have a lot of entries and I want to drop them into a dataframe to do some analysis on them with pandas.但是有些列表有很多条目,我想将它们放入 dataframe 中,用 pandas 对它们进行一些分析。

(['pE7464AFD1F'],
 [['t29', 1, 15, 50],
  ['t248', 1, 15, 15],
  ['t140', 1, 15, 33],
  ['t121', 1, 15, 41],
  ['t221', 1, 15, 19]])

Unless I'm misunderstanding your question, you have a tuple containing an identification number, and a list of lists which represent your data.除非我误解了您的问题,否则您有一个包含标识号的元组和一个代表您的数据的列表列表。

You're just looking to separate the two from each other, and turn the data into a dataframe.您只是想将两者分开,并将数据转换为 dataframe。

import pandas as pd

identifier, data = (['pE7464AFD1F'],
                    [['t29', 1, 15, 50],
                     ['t248', 1, 15, 15],
                     ['t140', 1, 15, 33],
                     ['t121', 1, 15, 41],
                     ['t221', 1, 15, 19]])

df = pd.DataFrame(data)

# For Display
print(df)

Output: Output:

      0  1   2   3
0   t29  1  15  50
1  t248  1  15  15
2  t140  1  15  33
3  t121  1  15  41
4  t221  1  15  19

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM