将 (720, 720) 的 Pandas DataFrame 重塑为 (518400, ) 2D 为 1D [英] Reshape a pandas DataFrame of (720, 720) into (518400, ) 2D into 1D

查看:54
本文介绍了将 (720, 720) 的 Pandas DataFrame 重塑为 (518400, ) 2D 为 1D的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个形状为 720*720 2D 的 DataFrame.我想将它转换为一维维度而不改变它的值.如何使用 Pandas 执行此操作?

I have a DataFrame with shape: 720*720 2D. I wanna convert it to 1D dimension without changing its values. How can I do this using Pandas?

推荐答案

使用 numpy.ravel 将 DataFrame 转换为 numpy 数组:

Use numpy.ravel with converted DataFrame to numpy array:

np.random.seed(123)
df = pd.DataFrame(np.random.randint(10, size=(3,3)))
print (df)
   0  1  2
0  2  2  6
1  1  3  9
2  6  1  0

out = df.values.ravel('F')
#alternative for pandas 0.24+
#out = df.to_numpy().ravel('F')
print (out)
[2 1 6 2 3 1 6 9 0]

s = pd.Series(df.values.ravel('F'))
#alternative for pandas 0.24+
#s = pd.Series(df.to_numpy().ravel('F'))
print (s)
0    2
1    1
2    6
3    2
4    3
5    1
6    6
7    9
8    0
dtype: int32

这篇关于将 (720, 720) 的 Pandas DataFrame 重塑为 (518400, ) 2D 为 1D的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆