Home > Enterprise >  rows of columns to column
rows of columns to column

Time:02-04

Would I need to use pivot to make a row of columns into one column? I have a dataset like the one shown but within each row are 8 separate rows. I need to make each cell its own row.

This would be an example of what i would start with:

d = {'col1':[1,9,17],'col2':[2,10,18],'col3':[3,11,19],'col4':[4,12,20],'col5':[5,13,21],'col6':[6,14,22],'col7':[7,15,23],'col8':[8,16,24]}
import pandas as pd
df = pd.DataFrame(data=d)

And then would need to have a new df like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

CodePudding user response:

You can stack the dataframe vertically:

new_df = pd.DataFrame({'col': df.stack().values})

or using the underlying raw array (.to_numpy().ravel()/.to_numpy().flatten()):

new_df = pd.DataFrame({'col': df.to_numpy().flatten()})

print(new_df)

    col
0     1
1     2
2     3
3     4
4     5
5     6
6     7
7     8
8     9
9    10
10   11
11   12
12   13
13   14
14   15
15   16
16   17
17   18
18   19
19   20
20   21
21   22
22   23
23   24

CodePudding user response:

You can also try using melt()

df1=df.melt().drop('variable', axis=1).sort_values('value')
  • Related