Python 如何选择数据框的最后一列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40144769/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to select the last column of dataframe
提问by Nate
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?).
这对我来说似乎很笨拙,而且不符合 Python 风格(而且很慢?)。
What is the easiest way to select the data for the last column in a pandas dataframe without specifying the name of the column?
在不指定列名的情况下为 Pandas 数据框中最后一列选择数据的最简单方法是什么?
回答by Boud
Use iloc and select all rows (:
) against the last column (-1
):
使用 iloc 并:
针对最后一列 ( -1
)选择所有行( ):
df.iloc[:,-1]
回答by jez
Somewhat similar to your original attempt, but more Pythonic, is to use Python's standard negative-indexing convention to count backwards from the end:
与您最初的尝试有些相似,但更 Python 化的是使用 Python 的标准负索引约定从末尾开始倒数:
df[df.columns[-1]]
回答by Anshul Singh Suryan
These are few things which will help you in understanding everything... using iloc
这些是一些可以帮助你理解一切的东西......使用 iloc
In iloc, [initial row:ending row, initial column:ending column]
在 iloc 中,[初始行:结束行,初始列:结束列]
case 1: if you want only last column --- df.iloc[:,-1] & df.iloc[:,-1:]
this means that you want only the last column...
情况 1:如果您只需要最后一列 ---df.iloc[:,-1] & df.iloc[:,-1:]
这意味着您只需要最后一列...
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...
情况 2:如果您想要除最后一列之外的所有列和所有行 ---df.iloc[:,:-1]
这意味着您想要除最后一列之外的所有列和所有行...
case 3: if you want only last row --- df.iloc[-1:,:] & df.iloc[-1,:]
this means that you want only the last row...
情况 3:如果您只需要最后一行 ---df.iloc[-1:,:] & df.iloc[-1,:]
这意味着您只需要最后一行...
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...
情况 4:如果您想要除最后一行之外的所有列和所有行 ---df.iloc[:-1,:]
这意味着您想要除最后一列之外的所有列和所有行...
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...
情况 5:如果您想要除最后一行和最后一列之外的所有列和所有行 ---df.iloc[:-1,:-1]
这意味着您想要除最后一列和最后一行之外的所有列和所有行...
回答by alEx
The question is: how to select the last column of a dataframe ? Appart @piRSquared, none answer the question.
问题是:如何选择数据框的最后一列?Appart @piRSquared,没有人回答这个问题。
the simplest way to get a dataframe with the last column is:
获取最后一列数据框的最简单方法是:
df.iloc[ :, -1:]
回答by piRSquared
df.T.iloc[-1]
df.T.tail(1)
pd.Series(df.values[:, -1], name=df.columns[-1])
回答by user28929304981
This is another way to do it. I think maybe a little more general:
这是另一种方法。我想也许更笼统一点:
df.ix[:,-1]
回答by Amit Sharma
Just to add to @Anshul Singh Suryan's answer:
只是添加到@Anshul Singh Suryan 的回答中:
When we split the dataframe to just get the last column:
当我们拆分数据框以获取最后一列时:
If we split like:
如果我们像这样拆分:
y = df.iloc[:,-1:] - y remains a dataframe
y = df.iloc[:,-1:] - y 仍然是一个 dataframe
However, if we split like
但是,如果我们像
y = df.iloc[:,-1] - y becomes a Series
.
y = df.iloc[:,-1] - y 变为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:]
这适用于您要提取的任意数量的行,而不仅仅是最后一行。例如,如果您想要n
数据帧的最后行数,其中 n 是小于或等于数据帧中列数的任何整数,那么您可以轻松执行以下操作: y = df.iloc[:,n :]
Replace 'n' by the number of columns you want. Same is true for rows as well.
用你想要的列数替换“n”。行也是如此。