简体   繁体   English

将 DataFrame 整形为 np.array

[英]Reshape DataFrame to np.array

Good afternoon, I have a dataframe with the dimension (963,1), how can I change something like this for a numpy format:下午好,我有一个维度为 (963,1) 的数据框,我如何为 numpy 格式更改这样的内容:


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当我使用 np.array (a) 时,在我看来,每个元素都在不同的行中


[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:制作一个 1 列框架:

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:来自它的数组是 (5,1) 形状的:

In [592]: df.values                                                                                  
Out[592]: 
array([[0],
       [1],
       [2],
       [3],
       [4]])

One column is a Series, which is 1d:一列是一个系列,它是 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:但是如果你有 (5,1) 形状数组,有很多方法可以重塑它:

In [596]: df.values.ravel()                                                                          
Out[596]: array([0, 1, 2, 3, 4])

ravel , flatten , reshape , squeeze , even indexing. ravelflattenreshapesqueeze ,甚至索引。 All these can be found in the basic numpy documentation.所有这些都可以在基本的numpy文档中找到。

Use values to convert dataframe to Numpy array.使用values将数据帧转换为 Numpy 数组。 Check the Docs.检查文档。

df.values

You can reshape if you want a flattened np array如果你想要一个扁平的 np 数组,你可以重塑

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]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM