如何选择数据帧的最后一列

我已经做了一些寻找这个问题的答案,但我所能想到的是:

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

对我来说,这看起来很笨重,而且不像蟒蛇(而且很慢?)。

在不指定列名的情况下,为熊猫数据框架中的最后一列选择数据的最简单的方法是什么?

188649 次浏览

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]]

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

df.iloc[:,-1:]
df.T.iloc[-1]

df.T.tail(1)

pd.Series(df.values[:, -1], name=df.columns[-1])

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

df.ix[:,-1]

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:]

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...

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.

Alternatively you can also use take:

df.take([-1], axis=1)