frank
frank

Reputation: 3608

convert from one column pandas dataframe to 3 columns based on index

I have:

    col1
0   1
1   2
2   3
3   4
4   5
5   6
...

I want, every 3 rows of the original dataframe to become a single row in the new dataframe:

    col1    col2    col3
0   1       2       3
1   4       5       6
...

Any suggestions?

Upvotes: 0

Views: 978

Answers (3)

agastya
agastya

Reputation: 376

The values of the dataframe are an array that can be reshaped using numpy's reshape method. Then, create a new dataframe using the reshaped values. Assuming your existing dataframe is df-

df_2 = pd.DataFrame(df.values.reshape(2, 3), columns=['col1', 'col2', 'col3'])

This will create the new dataframe of two rows and 3 columns.

   col1   col2  col3
0   0      1      2
1   3      4      5

Upvotes: 4

Kunal Sawant
Kunal Sawant

Reputation: 493

you can covert the col in numpy array and then reshape.

In [27]: np.array(df['col1']).reshape( len(df) // 3 , 3 )
Out[27]:
array([[1, 2, 3],
       [4, 5, 6]])

In [..] :reshaped_cols = np.array(df['col1']).reshape( len(df) // 3 , 3 )


pd.DataFrame( data = reshaped_cols , columns = ['col1' , 'col2' , 'col3' ] )


Out[30]:
   col1  col2  col3
0     1     2     3
1     4     5     6

Upvotes: 0

Ben.T
Ben.T

Reputation: 29635

You can use set_index and unstack to get the right shape, and add_preffix to change the column name:

print (df.set_index([df.index//3, df.index%3+1])['col1'].unstack().add_prefix('col'))
   col1  col2  col3
0     1     2     3
1     4     5     6

in case the original index is not consecutive values but you still want to reshape every 3 rows, replace df.index by np.arange(len(df)) for both in the set_index

Upvotes: 0

Related Questions