ash90
ash90

Reputation: 123

Reordering column index of a dataframe

I have a data frame of 15k columns. These columns are to be re-ordered. Such that. These columns are arranged as follows:

1) The first column should have values of third 2) The second column should have values of first 3) The third column should have values of first

Existing Column

0 1 2 3 4 5 6 7 8 .....14998 14999 15000

Desired Columns

2 0 1 5 3 4 8 6 7 .... 15000 14998 14999

Upvotes: 1

Views: 72

Answers (2)

csabinho
csabinho

Reputation: 1609

All you need is regex!

teststring=" ".join(str(i) for i in range(0,15000))
replacedstring=re.sub(r'((\d+) (\d+) (\d+))',r'\4 \2 \3',teststring)
replacedstring.split(" ")

Upvotes: 0

rafaelc
rafaelc

Reputation: 59284

IIUC, even simpler solution:

import itertools

k = [2, -1, -1] * (len(x)//3)
indexes = np.arange(15000) + np.array(k)

df.iloc[:, indexes]

Upvotes: 4

Related Questions