jake wong
jake wong

Reputation: 5228

unpacking of unequal list of lists into a pandas dataframe

I have the below list of lists in the variable mydata

[
  [["item1"], ["2016","value1"], ["2017","value2"], ["2018","value3"], ["2019","value4"]],
  [["item2"], ["2016","value1"], ["2017","value2"], ["2018","value3"], ["2019","value4"]],
  [["item3"], ["2016","value1"], ["2017","value2"], ["2018","value3"], ["2019","value4"]],
  [["item4"], ["2016","value1"], ["2017","value2"], ["2018","value3"], ["2019","value4"]],
]

I have been trying to put it into a dataframe such as below:

index     item1  | item2  | item3  | item4
2016      value1 | value1 | value1 | value1
2017      value2 | value2 | value2 | value2
2018      value3 | value3 | value3 | value3
2019      value4 | value4 | value4 | value4

However, when I tried DataFrame(mydata), it gives me the below result:

index   0                 | 1                 | 2                 | 3  
item1   ["2016","value1"] | ["2017","value2"] | ["2018","value3"] | ["2019","value4"]
item2   ["2016","value1"] | ["2017","value2"] | ["2018","value3"] | ["2019","value4"]
item3   ["2016","value1"] | ["2017","value2"] | ["2018","value3"] | ["2019","value4"]
item4   ["2016","value1"] | ["2017","value2"] | ["2018","value3"] | ["2019","value4"]

I think it is because of the nature of mydata where the list of lists is unequally distributed. But is it possible to unpack it into the dataFrame as i've specified? Or do I have to create a for loop to go through each element and unpack it to the dataFrame?

Upvotes: 0

Views: 91

Answers (1)

Chris
Chris

Reputation: 29742

One way by creating dict first:

df = pd.DataFrame({l[0][0]:dict(l[1:]) for l in mydata})
print(df)

Output:

       item1   item2   item3   item4
2016  value1  value1  value1  value1
2017  value2  value2  value2  value2
2018  value3  value3  value3  value3
2019  value4  value4  value4  value4

Upvotes: 2

Related Questions