Reshape a pandas DataFrame of (720, 720) into (518400, ) 2D into 1D

走远了吗. 提交于 2020-06-23 08:48:07

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/55062213/reshape-a-pandas-dataframe-of-720-720-into-518400-2d-into-1d

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!