重新排列數據框,每n行轉為列,從左到右,由上而下

2024-09-29 01:36:15 发布

您现在位置:Python中文网/ 问答频道 /正文

字符串列表

enter image description here

我想把它们排列成下面的格式。每3行一组,从左到右,从上到下:

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

怎么解决?谢谢你

请注意,“行#”和“字符串”仅用于清晰阅读。我只想实现内容的重新排列


Tags: 字符串importdfdatahereismyas