Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What would be Python/pandas equivalent of this R code for rearranging columns of a dataframe? [duplicate]

data<-data[c(8,1:7)]

Basically, move the last column to the first position. How would I do this in Python by using only column indices, preferably in a single line?

like image 864
HMK Avatar asked Dec 03 '22 23:12

HMK


1 Answers

use numpy.r_
This was intended to allow R like syntax for array slicing. Hence the r_

Assuming your dataframe is named df

import numpy as np
import pandas as pd

df = df.iloc[:, np.r_[8, 1:7]]
like image 82
piRSquared Avatar answered Dec 11 '22 16:12

piRSquared