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.