我有以下代码:
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)
这是我得到的当前结果:
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)
我想得到下面的熊猫数据框:
(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
您将在类中看到,属性标签具有需要使用的相应列的名称。
答案 0 :(得分:3)
尝试一下:
df.set_index('label').T.apply(pd.Series.explode).reset_index(drop=True)
输出:
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
答案 1 :(得分:2)
使用unstack
并从中构造一个新的数据框
df_final = pd.DataFrame.from_dict(df.set_index('label').unstack().droplevel(0)
.to_dict())
或从label
和vector
构造一个字典,并将其传递给数据框构造器
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