Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select the last column of dataframe

Tags:

python

pandas

I have done some searching for the answer to this question, but all I can figure out is this:

df[df.columns[len(df.columns)-1]] 

which to me seems unweildy, and un-pythonic (and slow?).

What is the easiest way to select the data for the last column in a pandas dataframe without specifying the name of the column?

like image 903
Nate Avatar asked Oct 20 '16 03:10

Nate


People also ask

How do I select all columns except last one?

To select all columns except one column in Pandas DataFrame, we can use df. loc[:, df. columns != <column name>].

How do you find the last record of a data frame?

Select & print last row of dataframe using tail() It will return the last row of dataframe as a dataframe object. Using the tail() function, we fetched the last row of dataframe as a dataframe and then just printed it.


1 Answers

Use iloc and select all rows (:) against the last column (-1):

df.iloc[:,-1:] 
like image 132
Zeugma Avatar answered Sep 23 '22 20:09

Zeugma