[英]Converting a column having numpy arrays convert it into a numpy array with dtype as object
I have a column in a dataframe having numpy arrays of length 10. My dataframe is like this: 我在一个数据帧中有一列,该数据帧的长度为10个numpy数组。我的数据帧如下所示:
0 [2.0, 1246.0, 82.0, 43.0, 569.0, 46.0, 424.0, ...
1 [395.0, 2052.0, 1388.0, 8326.0, 5257.0, 176.0,...
10 [4.0, 1.0, 13.0, 1409.0, 7742.0, 259.0, 1856.0...
100 [4.0, 87.0, 1595.0, 706.0, 2935.0, 6028.0, 442...
1000 [45.0, 582.0, 124.0, 6530.0, 6548.0, 748.0, 61...
Name: embedding1, dtype: object
When I am converting it into a numpy array of array using this: 当我使用此将其转换为numpy数组时:
input = np.asarray(df.tolist())
It is giving the array like this: 它给出了这样的数组:
array([array([ 2., 1246., 82., 43., 569., 46., 424., 446., 1054., 39.]),
array([4.0000e+00, 1.0000e+00, 1.3000e+01, 1.4090e+03, 7.7420e+03,
2.5900e+02, 1.8560e+03, 3.6181e+04, 4.2000e+01, 8.9000e+02]),
...,
array([4.000e+00, 1.000e+00, 1.300e+01, 2.900e+01, 4.930e+02, 2.760e+02,1.100e+01, 6.770e+02, 6.740e+02, 5.806e+03]),], dtype=object)
The type it is giving is object. 它给出的类型是对象。 I want the object as float because it is giving the shape(1000,) but I want shape as (1000,10). 我希望该对象为float,因为它的形状为(1000,),但我希望形状为(1000,10)。 I have tried using this: 我试过使用此:
input1 = np.asarray(df1.tolist(),dtype=np.float)
But it is giving the following error: 但是它给出了以下错误:
ValueError: setting an array element with a sequence.
How to solve this? 如何解决呢?
PS: All the elements of the dataframe's row numpy array are of float type PS:数据框的行numpy数组的所有元素均为浮点类型
First of all, it seems like you have a pd.Series
and not a data frame. 首先,似乎您有一个pd.Series
而不是一个数据框。
Take the setup : 进行设置:
x = [[2.0, 1246.0, 82.0, 43.0, 569.0, 46.0, 424.0],
[395.0, 2052.0, 1388.0, 8326.0, 5257.0, 176.0],
[4.0, 1.0, 13.0, 1409.0, 7742.0, 259.0, 1856.0],
[4.0, 87.0, 1595.0, 706.0, 2935.0, 6028.0, 442],
[45.0, 582.0, 124.0, 6530.0, 6548.0, 748.0, 61]]
s = pd.Series(x)
Which yields 哪个产量
0 [2.0, 1246.0, 82.0, 43.0, 569.0, 46.0, 424.0]
1 [395.0, 2052.0, 1388.0, 8326.0, 5257.0, 176.0]
2 [4.0, 1.0, 13.0, 1409.0, 7742.0, 259.0, 1856.0]
3 [4.0, 87.0, 1595.0, 706.0, 2935.0, 6028.0, 442]
4 [45.0, 582.0, 124.0, 6530.0, 6548.0, 748.0, 61]
dtype: object
You have a pd.Series
of arrays. 您有数组的pd.Series
。 And it seems like you want to flatten it. 好像您想将其展平。 Using the default constructor in a list of lists yields a data frame where each list is interpreted as a row: 在列表列表中使用默认构造函数会产生一个数据框,其中每个列表都被解释为一行:
df2 = pd.DataFrame(s.tolist())
0 1 2 3 4 5 6
0 2.0 1246.0 82.0 43.0 569.0 46.0 424.0
1 395.0 2052.0 1388.0 8326.0 5257.0 176.0 NaN
2 4.0 1.0 13.0 1409.0 7742.0 259.0 1856.0
3 4.0 87.0 1595.0 706.0 2935.0 6028.0 442.0
4 45.0 582.0 124.0 6530.0 6548.0 748.0 61.0
Now you can just get the underlying np.array
accessing the data frame .values
现在,你可以得到下面的np.array
访问数据帧.values
df2.values
array([[2.000e+00, 1.246e+03, 8.200e+01, 4.300e+01, 5.690e+02, 4.600e+01,
4.240e+02],
[3.950e+02, 2.052e+03, 1.388e+03, 8.326e+03, 5.257e+03, 1.760e+02,
nan],
[4.000e+00, 1.000e+00, 1.300e+01, 1.409e+03, 7.742e+03, 2.590e+02,
1.856e+03],
[4.000e+00, 8.700e+01, 1.595e+03, 7.060e+02, 2.935e+03, 6.028e+03,
4.420e+02],
[4.500e+01, 5.820e+02, 1.240e+02, 6.530e+03, 6.548e+03, 7.480e+02,
6.100e+01]])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.