重新排列数据帧,每n行到列,从左到右,自上而下

问题描述 投票:-1回答:1

字符串列表。

enter image description here

我想将它们排列成以下格式。每三行一组,从左到右,从上到下:

enter image description here

通过重塑,我只能做到以下几点:

import pandas as pd
import numpy as np

data = [

"by Emily Dickinson",
"There is another sky,",
"Ever serene and fair,",
"And there is another sunshine,",
"Though it be darkness there;",
"Never mind faded forests, Austin,",
"Never mind silent fields -",
"Here is a little forest,",
"Whose leaf is ever green;",
"Here is a brighter garden,",
"Where not a frost has been;",
"In its unfading flowers",
"I hear the bright bee hum:",
"Prithee, my brother,",
"Into my garden come!"] 

df = pd.DataFrame(data)

df_1 = pd.DataFrame(np.reshape(df.values,(5,3)))

enter image description here

如何解决?谢谢。

P.s。 “ Row#”和“ String”仅用于清晰阅读。我只想实现内容的重新排列。

python pandas numpy dataframe
1个回答
1
投票

只需更改重塑参数和通过顺序参数

df_1 = pd.DataFrame(np.reshape(df.values,(3,5),order='F'))
© www.soinside.com 2019 - 2024. All rights reserved.