Nathan Miller Nathan Miller - 1 month ago 6
Python Question

How to select the last column of dataframe

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?

Answer

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

test.iloc[:,-1]