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

Viewed 53

I have a function that returns a list of lists (see image below). 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. I know how to take an entire list of lists and create a df.

data = lists
df = pd.DataFrame(data)

Can anyone help me make a dataframe minus the first list? 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.

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

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.

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:

      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
Related