-2

I have a dataframe

       0     1       2     3   ............  1041   1042   1043
0    32.5   19.4   66.6   91.4               55.5   10.4   77.2   
1    13.3   85.3   22.4   65.8               23.4   90.2   14.5   
2    22.4   91.7   57.1   23.5               58.2   81.5   46.7   
3    75.7   47.1   91.4   45.2               89.7   38.7   78.3   
.
.
.
.
18   32.5   23.4   90.2   14.5               91.7   57.1   23.5   
19   56.7   58.2   81.5   46.7               65.5   43.4   76.2   
20   76.8   19.4   66.6   91.4               54.9   60.4   96.4   

the dataframe has 20 rows and 1044 columns. i want to append/concat 2nd row with first row and 4th row with 3rd row and 6th row with 5th row and so on. in this way, the dataframe will become 10 rows and 2088 columns.

After this, from 0 to 1043 columns label should be rename as x1,x2,x3, ...., x1043 and from 1044 to 2083 columns should be rename as y1,y2,y3,.... , y20837. Now the updated dataframe looks like as follow

      X0    X1    X2 .... X1042  X1043   Y0    Y1    Y2 .... Y2086    Y2087         
0    32.5  19.4  66.6 ... 10.4   77.2   13.3  85.3  22.4 ...  90.2    14.5
1    22.4  91.7  57.1 ... 81.5   46.7   75.7  47.1  91.4 ...  38.7    78.3 
.
.
.
.
10   56.7  58.2  81.5 ... 43.4   76.2   76.8   19.4  66.6 ... 60.4   96.4 

how to do achieve these two tasks? I have tried a lot (using append/concat/join functions) but still unsuccessful to do this.

James Z
  • 12,209
  • 10
  • 24
  • 44
  • Can you share your progress till now? – ss3387 Sep 06 '22 at 11:04
  • 3
    Don't describe what the data looks like, show it - in the form of a small sample, as text, not as image. See [How to make good reproducible pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples). – fsimonjetz Sep 06 '22 at 11:06
  • Share your python code till now so we can fix it. At least share the data. – ss3387 Sep 06 '22 at 11:06
  • Please provide enough code so others can better understand or reproduce the problem. – Community Sep 06 '22 at 11:14

1 Answers1

0

Some manipulation of the column names and a trick with indexes is needed but sure it can be done. Remember to pay attention to the behavior when the number of columns is odd.

import numpy as np
import pandas as pd

# Create dummy data
df = pd.DataFrame(np.random.randint(0, 100, size=(1044, 20)))

def pairwise(it):
    a = iter(it)
    return zip(a, a)

l = [l for l, r in pairwise(df.columns)]
r = [r for l, r in pairwise(df.columns)]

# Rename columns of one of the DFs otherwise the concat will overlap them
dfr = df[r].rename(columns={r: l for l, r in pairwise(df.columns)})

# Concat vertically, ignore index to have them stacked sequentially
df = pd.concat([df[l], dfr], axis=0, ignore_index=True)
Newbie
  • 4,462
  • 11
  • 23
  • Thanks for your great help... I tried to convert this code into 20 rows and 1044 columns as input dataframe and the resultant dataframe should have 10 rows and 2088 columns but didn't achieve this yet... Can you guide me how to do this...? – Saghir Ahmed Sep 06 '22 at 13:01
  • Should work, as long as you assign your data to the initial `df`. – Newbie Sep 06 '22 at 22:40