pandas 如何选择除数据框 n Python 的最后 3 列之外的所有列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/53608653/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 06:11:41  来源:igfitidea点击:

How to select all but the 3 last columns of a dataframe n Python

pythonpandas

提问by Vincent

I want to select all but the 3 last columns of my dataframe.

我想选择数据框的最后 3 列以外的所有列。

I tried :

我试过 :

df.loc[:,-3]

But it does not work

但它不起作用

Edit : title

编辑:标题

回答by Mayank Porwal

Select everything EXCEPT the last 3 columns, do this using iloc:

选择除最后 3 列之外的所有内容,使用iloc

In [1639]: df
Out[1639]: 
   a  b  c  d  e
0  1  3  2  2  2
1  2  4  1  1  1

In [1640]: df.iloc[:,:-3]
Out[1640]: 
   a  b
0  1  3
1  2  4

回答by U10-Forward

Use this df.columnsbeing sliced, and putted into a df[...]bracket:

使用这个df.columns被切片,并放入df[...]括号中:

print(df[df.columns[:-3]])