Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selecting last n columns and excluding last n columns in dataframe

How do I:

  1. Select last 3 columns in a dataframe and create a new dataframe?

I tried:

y = dataframe.iloc[:,-3:] 
  1. Exclude last 3 columns and create a new dataframe?

I tried:

X = dataframe.iloc[:,:-3] 

Is this correct?

I am getting array dimensional errors further in my code and want to make sure this step is correct.

Thank you

like image 463
Toly Avatar asked Oct 09 '15 16:10

Toly


1 Answers

The most efficient way:

1. Select last n columns

df1 = df.iloc[:,-n:]

2. Exclude last n columns

df1 = df.iloc[:,:-n]

like image 167
Decision Scientist Avatar answered Sep 20 '22 20:09

Decision Scientist