这个例外来自于您希望在存储int
s的列(array
)中插入list
或array
这一事实。并且Pandas和NumPy中的array
不能有“粗糙的形状”,因此不能在一行中包含2个元素,而在其他所有其他元素中(除了可能使用掩码)。
为了使它工作,你需要存储“一般”对象。例如:
import pandas as pd
df = pd.DataFrame({'col1' : [25, 12, 14, 21]})
df.col1[0] = [1, 2]
# ValueError: setting an array element with a sequence.
但这个工程:
>>> df.col1 = df.col1.astype(object)
>>> df.col1[0] = [1, 2]
>>> df
col1
0 [1, 2]
1 12
2 14
3 21
注:我不建议这样做,由于object
列是不是专门类型的列慢得多。但是因为你用for
循环遍历Column,所以你不需要性能,所以你也可以使用object
数组。
如果你想让它快是矢量化convert2vector
功能和结果分配给两列,你应该做的事情:
import pandas as pd
import numpy as np
def convert2Vector(angle):
"""I don't know what your function does so this is just something that
calculates the sin and cos of the input..."""
ret = np.zeros((angle.size, 2), dtype=float)
ret[:, 0] = np.sin(angle)
ret[:, 1] = np.cos(angle)
return ret
>>> df = pd.DataFrame({'col1' : [25, 12, 14, 21]})
>>> df['col2'] = [0]*len(df)
>>> df[['col1', 'col2']] = convert2Vector(df.col1)
>>> df
col1 col2
0 -0.132352 0.991203
1 -0.536573 0.843854
2 0.990607 0.136737
3 0.836656 -0.547729
谢谢!然后为了使用矢量的元素作为实数,我需要再次转换它们吗? –
@ BenJo你的意思是在第二种情况下还是第一种情况?在第二种情况下很容易:只需在需要第一项的'col1'和需要第二项的'col2'处使用。如果你在谈论第一种情况,你可以使用'np.array(df。col1)'将其转换为'rows x 2'数组:) – MSeifert
2列解决方案的问题在于,我必须将数据集导入数据库中,并使用定义的列数,以便第一个解决方案更好。特别是如果我仍然可以作为实数访问向量的元素 –