111

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?

Nate
  • 2,113
  • 5
  • 19
  • 25

8 Answers8

205

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

df.iloc[:,-1:]
svet
  • 11,078
  • 1
  • 15
  • 26
Zeugma
  • 31,231
  • 9
  • 69
  • 81
49

Somewhat similar to your original attempt, but more Pythonic, is to use Python's standard negative-indexing convention to count backwards from the end:

df[df.columns[-1]]
jez
  • 14,867
  • 5
  • 37
  • 64
30

These are few things which will help you in understanding everything... using iloc

In iloc, [initial row:ending row, initial column:ending column]

case 1: if you want only last column --- df.iloc[:,-1] & df.iloc[:,-1:] this means that you want only the last column...

case 2: if you want all columns and all rows except the last column --- df.iloc[:,:-1] this means that you want all columns and all rows except the last column...

case 3: if you want only last row --- df.iloc[-1:,:] & df.iloc[-1,:] this means that you want only the last row...

case 4: if you want all columns and all rows except the last row --- df.iloc[:-1,:] this means that you want all columns and all rows except the last column...

case 5: if you want all columns and all rows except the last row and last column --- df.iloc[:-1,:-1] this means that you want all columns and all rows except the last column and last row...

stardust
  • 343
  • 3
  • 17
Anshul Singh Suryan
  • 878
  • 1
  • 11
  • 15
9

Just to add to @Anshul Singh Suryan's answer:

When we split the dataframe to just get the last column:

If we split like:

y = df.iloc[:,-1:] - y remains a dataframe

However, if we split like

y = df.iloc[:,-1] - y becomes a Series.

This is a notable difference that I've found in the two approaches. If you don't care about the resultant type, you can use either of the two. Otherwise you need to take care of the above findings.

This is applicable for any number of rows you want to extract and not just the last row. For example, if you want last n number of rows of a dataframe, where n is any integer less than or equal to the number of columns present in the dataframe, then you can easily do the following:

y = df.iloc[:,n:]

Replace n by the number of columns you want. Same is true for rows as well.

heilala
  • 770
  • 8
  • 19
Amit Sharma
  • 690
  • 8
  • 22
7

The question is: how to select the last column of a dataframe ? Appart @piRSquared, none answer the question.

the simplest way to get a dataframe with the last column is:

df.iloc[ :, -1:]
alEx
  • 193
  • 6
  • 12
3
df.T.iloc[-1]

df.T.tail(1)

pd.Series(df.values[:, -1], name=df.columns[-1])
piRSquared
  • 285,575
  • 57
  • 475
  • 624
  • 1
    I don't see any value of building the Series explicitly out of the numpy values extraction compared to a direct call to iloc – Zeugma Oct 20 '16 at 12:42
  • @Boud, There are many ways to do this. I was simply trying to dump a bunch down. Admittedly, the third option is the weakest. – piRSquared Oct 20 '16 at 12:58
1

This is another way to do it. I think maybe a little more general:

df.ix[:,-1]
  • 1
    In this case, your answer is not recommended because the question asks explicitly for the *last* column which is index location based. `ix` mixes label and index location accessor. See [here](http://stackoverflow.com/questions/31593201/pandas-iloc-vs-ix-vs-loc-explanation) for more. – pansen Mar 24 '17 at 19:10
  • 4
    Indeed, in addition, `ix` is deprecated by now. – Archie Apr 12 '18 at 07:59
0

Alternatively you can also use take:

df.take([-1], axis=1)
rachwa
  • 1,805
  • 1
  • 14
  • 17