I have a dataframe:
col1 col2 col3 col4 col5 col6 col7 col8 col9
a1 b1 c1 d1 e1 f1 g1 h1 i1
a2 b2 c2 d2 e2 f2 g2 h2 i2
I want to to turn every three column into rows:
col1 col2 col3
a1 b1 c1
a2 b2 c2
d1 e1 f1
d2 e2 f2
g1 h1 i1
g2 h2 i2
how to do that?
CodePudding user response:
You can simply reshape the .values
of the dataframe and use the reshaped array to create a new dataframe:
df_reshaped = pd.DataFrame(df.values.reshape((-1, 3)), columns=df.columns[:3])
The shape
argument to reshape
has -1
in as the first dimension because we want as many rows as can be made from the original data with three columns.
which gives:
col1 col2 col3
0 a1 b1 c1
1 d1 e1 f1
2 g1 h1 i1
3 a2 b2 c2
4 d2 e2 f2
5 g2 h2 i2
CodePudding user response:
Use df.values.reshape
to change the shape of the data frame and then assign the column names.
CODE
import pandas as pd
df = pd.DataFrame({"col1": ["a1", "a2"], "col2": ["b1", "b2"], "col3": ["c1", "c2"],
"col4": ["d1", "d2"], "col5": ["e1", "e2"], "col6": ["f1", "f2"],
"col7": ["g1", "g2"], "col8": ["h1", "h2"], "col9": ["i1", "i2"]})
newdf = pd.DataFrame(df.values.reshape(-1, 3), columns=['col1', 'col2', 'col3'])
print(newdf)
OUTPUT
col1 col2 col3
0 a1 b1 c1
1 d1 e1 f1
2 g1 h1 i1
3 a2 b2 c2
4 d2 e2 f2
5 g2 h2 i2