Home > Software design >  Convert list to dataframe (Pandas)
Convert list to dataframe (Pandas)

Time:12-19

Is there any way I can convert this type of output into a Dataframe? Output:

[['A', 'B'], ['Orange', 'Apple']]
[['C', 'D'], ['Coconut', 'Lemon']]
[['E'], ['Mango', 'Strawberry']]
I want to turn the output into a dataframe like this:

`     x1         X2
0   A,B   Orange, Apple
1   C,D   Coconut, Lemon
2   E     Mango, Strawberry`

CodePudding user response:

Just add this data to a list

import pandas as pd

data = [[['A', 'B'], ['Orange', 'Apple']],
        [['C', 'D'], ['Coconut', 'Lemon']],
        [['E'], ['Mango', 'Strawberry']]]

df = pd.DataFrame(data, columns=['x1', 'x2'])

so df looks like

       x1                   x2
0  [A, B]      [Orange, Apple]
1  [C, D]     [Coconut, Lemon]
2     [E]  [Mango, Strawberry]

CodePudding user response:

Simple fix:

pd.DataFrame(
    {"x1":[["A", "B"], ["C", "D"], ["E"]],
    "x2":[["Orange", "Apple"], ["Coconut", "Lemon"], ["Mango", "Strawberry"]]}
)
  • Related