python : reform multiple columns into two columns

Question:

I would like to reform the following dataframe

main data

into :

reformed data

Could somebody help me with that, please?

Asked By: Amir Rastgo

||

Answers:

A screenshot is not a good format for community to reproduce your code, and data.

That is a shape(m, n) to shape(u, v) problem, probably you don’t know the term in Pandas, Numpy, but still can trynna google "reform", or "reshape".

import pandas as pd
import numpy as np
df.shape
###
(9, 12)

enter image description here

We can use np.hsplit() and np.vstack() to deal with,

shape calculation: (9 × 12) = c×(9 × n), with n = 2

# expected output columns  
n = 2

output = np.vstack(np.hsplit(df.values, df.shape[1]/n))
output_df = pd.DataFrame(output, columns=['node1','node2'])
print(output_df.head(10))
###
   node1  node2
0  38.47  37.86
1  37.63  37.02
2  38.27  37.66
3  38.64  38.03
4  38.12  37.51
5  37.57  36.96
6  37.83  37.22
7  37.62  37.01
8  37.04  36.43
9  40.17  39.52
Answered By: Baron Legendre

You can use the underlying numpy array:

n = 2

df = pd.DataFrame(df.values.reshape(-1,df.shape[1]//n, n)
                    .reshape(-1, n, order='F'),
                  columns=[f'node{i+1}' for i in range(n)])

Example:

   node1  node2
0      0      1
1      6      7
2      2      3
3      8      9
4      4      5
5     10     11

Used input:

   0  1  2  3   4   5
0  0  1  2  3   4   5
1  6  7  8  9  10  11

NB. The input must have an even number of columns (or multiple of n for the generic case).

Answered By: mozway