Good afternoon, I have a dataframe with the dimension (963,1), how can I change something like this for a numpy format:
array ([244.1462534,212.68483386, 212.04058487, 236.39615555]
when I use, np.array (a), it appears to me, each element in a different row
[244.1462534,
244.1462534,
244.1462534,
244.1462534]
as there are many it is difficult to see it like this
Make a 1 column frame:
In [590]: df = pd.DataFrame(np.arange(5), columns=['x'])
In [591]: df
Out[591]:
x
0 0
1 1
2 2
3 3
4 4
The array from that is (5,1) shaped:
In [592]: df.values
Out[592]:
array([[0],
[1],
[2],
[3],
[4]])
One column is a Series, which is 1d:
In [594]: df['x']
Out[594]:
0 0
1 1
2 2
3 3
4 4
Name: x, dtype: int64
In [595]: df['x'].values
Out[595]: array([0, 1, 2, 3, 4])
But if you have the (5,1) shape array, there are lots of ways of reshaping it:
In [596]: df.values.ravel()
Out[596]: array([0, 1, 2, 3, 4])
ravel
, flatten
, reshape
, squeeze
, even indexing. All these can be found in the basic numpy
documentation.
Use values
to convert dataframe to Numpy array. Check the Docs.
df.values
You can reshape if you want a flattened np array
df = pd.DataFrame(np.random.randn(10))
print (df.shape)
print (df.values.reshape(-1))
Output:
(10, 1)
[-1.43902815 0.72724325 -0.36741276 -1.96696158 0.5852711 -2.03214297
0.11657485 -1.77276773 0.33315229 -1.37454383]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.