Python 熊猫:添加数组作为列的值
我有一个熊猫数据框Python 熊猫:添加数组作为列的值,python,pandas,Python,Pandas,我有一个熊猫数据框df,它存储一些数值: print(df) value 0 0 1 2 2 4 3 5 4 8 我有一个函数,它把一个数值转换成一个热向量 print(to_categorical(0)) [1 0 0 0 0 0 0 0 0 0] print(to_categorical(5)) [0 0 0 0 0 5 0 0 0 0] 等等 因此,我可以在数值列上调用函数:
df
,它存储一些数值:
print(df)
value
0 0
1 2
2 4
3 5
4 8
我有一个函数,它把一个数值转换成一个热向量
print(to_categorical(0))
[1 0 0 0 0 0 0 0 0 0]
print(to_categorical(5))
[0 0 0 0 0 5 0 0 0 0]
等等
因此,我可以在数值列上调用函数:
print(to_categorical(df['value'))
[[1 0 0 0 0 0 0 0 0 0]
[0 0 1 0 0 0 0 0 0 0]
[0 0 0 0 1 0 0 0 0 0]
[0 0 0 0 0 1 0 0 0 0]
[0 0 0 0 0 0 0 0 1 0]]
现在我想将结果存储为一个新列。以下是我对我的示例的期望:
df['one-hot'] = to_categorical(df['value')
print(df)
value one-hot
0 0 [1 0 0 0 0 0 0 0 0 0]
1 2 [0 0 1 0 0 0 0 0 0 0]
2 4 [0 0 0 0 1 0 0 0 0 0]
3 5 [0 0 0 0 0 1 0 0 0 0]
4 8 [0 0 0 0 0 0 0 0 1 0]
但这给了我一个错误,因为pandas试图将我的阵列展平为多个列。我该怎么做呢?首先,我认为在pandas中使用
列表
s是不可能的,但可以通过转换为列表:
df['one-hot'] = to_categorical(df['value').tolist()
df['one-hot']=to_category(df['value')。tolist()
可能的重复在我的例子中,我希望有一个结构来存储我的值(几千个唯一值)和对应的一个hot向量(几千个值的向量)之间的映射你认为什么是更好的方法?@Nakeuh-更好的方法是创建新的数据帧-df1=pd.DataFrame(to_categorical(df['value'),index=df.index)
Hm,我明白了。我想“一体式数据帧”的计算效率较低?我想我现在还是采用非高效的方法吧(我更喜欢只有一个对象,我并不真正关心用例中的效率),但我会记住你对未来的建议。谢谢!