All,
I have a pandas dataframe with ~30 rows and 1 column. When I display it in Jupyter, all 30 rows are displayed in one long list. I am looking for a way to wrap the rows into multiple displayed columns, such as below:
Example dataframe:
df = pd.DataFrame([
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
'u', 'v', 'w', 'x', 'y', 'z', 'aa', 'ab', 'ac', 'ad'],
columns=['value'])
Example output
value value value
0 a 10 k 20 u
1 b 11 l 21 v
2 c 12 m 22 w
3 d 13 n 23 x
4 e 14 o 24 y
5 f 15 p 25 z
6 g 16 q 26 aa
7 h 17 r 27 ab
8 i 18 s 28 ac
9 j 19 t 29 ad
CodePudding user response:
Try
df[['col1', 'col2']] = df['col'].str.split(' ', 1, expand=True)
CodePudding user response:
You can use this helper function:
def reshape(df, rows=10):
length = len(df)
cols = np.ceil(length / rows).astype(int)
df = df.assign(rows=np.tile(np.arange(rows), cols)[:length],
cols=np.repeat(np.arange(cols), rows)[:length]) \
.pivot('rows', 'cols', df.columns.tolist()) \
.sort_index(level=1, axis=1).droplevel(level=1, axis=1).rename_axis(None)
return df
Output
>>> reshape(df)
value value value
0 a k u
1 b l v
2 c m w
3 d n x
4 e o y
5 f p z
6 g q aa
7 h r ab
8 i s ac
9 j t ad
