Arman Mojaver
Arman Mojaver

Reputation: 451

Create pandas Dataframe from list of objects

I have the following code:

import numpy as np
import pandas as pd

class my_class:
    def __init__(self, vector, par1, par2, par3):
        self.vector = vector  # this is a 1D numpy object.
        self.label = '({}, {}, {})'.format(par1, par2, par3)

obj1 = my_class(np.array([0, 1, 2, 3]), 4, -8, 7)
obj2 = my_class(np.array([6, 7, 8, 9]), 10, 4, 15)
obj3 = my_class(np.array([-1, -2, 3, -4]), 9, 3, 6)
obj4 = my_class(np.array([-10, -15, -20, 3]), 1, -2, 6)

my_list_of_objects = [obj1, obj2, obj3, obj4]

df = pd.DataFrame([o.__dict__ for o in my_list_of_objects])
print(df)

This is the current result that im getting:

               vector        label
0        [0, 1, 2, 3]   (4, -8, 7)
1        [6, 7, 8, 9]  (10, 4, 15)
2     [-1, -2, 3, -4]    (9, 3, 6)
3  [-10, -15, -20, 3]   (1, -2, 6)

and I would like to get the follwing pandas Dataframe:

   (4, -8, 7)   (10, 4, 15)   (9, 3, 6)   (1, -2, 6)
0       0            6           -1          -10
1       1            7           -2          -15
2       2            8            3          -20
3       3            9           -4            3

As you can see in the class, the attribute label has the name of the corresponding column that needs to be used.

Upvotes: 2

Views: 430

Answers (2)

Andy L.
Andy L.

Reputation: 25239

Use unstack and construct a new dataframe from it

df_final = pd.DataFrame.from_dict(df.set_index('label').unstack().droplevel(0)
                                                                 .to_dict())

Or construct a dictionary from label and vector and pass it to dataframe constructor

df_final = pd.DataFrame.from_dict(dict(df[['label', 'vector']].to_numpy()))

Out[267]:
   (4, -8, 7)  (10, 4, 15)  (9, 3, 6)  (1, -2, 6)
0           0            6         -1         -10
1           1            7         -2         -15
2           2            8          3         -20
3           3            9         -4           3

Upvotes: 2

Scott Boston
Scott Boston

Reputation: 153460

Try this:

df.set_index('label').T.apply(pd.Series.explode).reset_index(drop=True)

Output:

label (4, -8, 7) (10, 4, 15) (9, 3, 6) (1, -2, 6)
0              0           6        -1        -10
1              1           7        -2        -15
2              2           8         3        -20
3              3           9        -4          3

Upvotes: 2

Related Questions