Pandas - 删除 DataFrame 的最后一列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45196965/
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
Pandas - drop last column of DataFrame
提问by theprowler
I have a DataFrame and I would like to drop the last column of it. Until now I had just been dropping what I believed to be the final column with
我有一个 DataFrame,我想删除它的最后一列。直到现在,我才刚刚放弃了我认为是最后一栏的内容
if len(fish_frame.columns) == 4:
del fish_frame[3]
.
if len(fish_frame.columns) == 4:
del fish_frame[3]
.
Right before this command, however, I drop all columns of NaN
s. So that removes column [3]
because it is filled with NaN
s, so it fails.
然而,就在此命令之前,我删除了NaN
s 的所有列。这样就删除了列,[3]
因为它充满了NaN
s,所以它失败了。
I would like to say just drop the final column of the entire DataFrame. I feel like that would work perfectly.
我想说只是删除整个 DataFrame 的最后一列。我觉得那会很完美。
I tried fish_frame([:-1], axis=1)
but that's invalid syntax.
我试过了,fish_frame([:-1], axis=1)
但这是无效的语法。
Any help would be appreciated thanks.
任何帮助将不胜感激谢谢。
The DataFrame:
数据框:
fish_frame after dropna:
0 1 2 4
0 #0721 NaN NaN NaN
1 GBE COD 746 .00 ,492.00
2 GBW COD 13,894 fish_frame = fish_frame.iloc[:, :-1]
0 1 2
0 #0721 NaN NaN
1 GBE COD 746 .00
2 GBW COD 13,894 fish_frame = fish_frame.drop(fish_frame.columns[-1],axis=1)
.50
3 GOM COD 60 .00
4 GB WINTER FLOUNDER 94,158 0 1 2
0 #0721 NaN NaN
1 GBE COD 746 .00
2 GBW COD 13,894 df = df.iloc[:,:-1]
.50
3 GOM COD 60 .00
4 GB WINTER FLOUNDER 94,158 df = df.drop(df.columns[column_index],axis=1)
.25
5 GOM WINTER FLOUNDER 3,030 df = df.drop(['column_name'],axis =1)
.50
6 GBE HADDOCK 18,479 ##代码##.02
7 GOM HADDOCK 0 ##代码##.02
8 GBW HADDOCK 110,470 ##代码##.02
9 HAKE 259 .30
10 PLAICE 3,738 ##代码##.40
11 POLLOCK 3,265 ##代码##.02
12 WITCH FLOUNDER 1,134 .30
13 SNE YT 1,458 ##代码##.65
14 GB YT 4,499 ##代码##.70
15 REDFISH 841 ##代码##.02
16 54 DAS @ .00/DAY = 432.00 NaN NaN
.25
5 GOM WINTER FLOUNDER 3,030 ##代码##.50
6 GBE HADDOCK 18,479 ##代码##.02
7 GOM HADDOCK 0 ##代码##.02
8 GBW HADDOCK 110,470 ##代码##.02
9 HAKE 259 .30
10 PLAICE 3,738 ##代码##.40
11 POLLOCK 3,265 ##代码##.02
12 WITCH FLOUNDER 1,134 .30
13 SNE YT 1,458 ##代码##.65
14 GB YT 4,499 ##代码##.70
15 REDFISH 841 ##代码##.02
16 54 DAS @ .00/DAY = 432.00 NaN NaN
.50 ,947.00
3 GOM COD 60 .00 0.00
4 GB WINTER FLOUNDER 94,158 ##代码##.25 ,539.50
5 GOM WINTER FLOUNDER 3,030 ##代码##.50 ,515.00
6 GBE HADDOCK 18,479 ##代码##.02 9.58
7 GOM HADDOCK 0 ##代码##.02 ##代码##.00
8 GBW HADDOCK 110,470 ##代码##.02 ,209.40
9 HAKE 259 .30 6.70
10 PLAICE 3,738 ##代码##.40 ,495.20
11 POLLOCK 3,265 ##代码##.02 .30
12 WITCH FLOUNDER 1,134 .30 ,474.20
13 SNE YT 1,458 ##代码##.65 7.70
14 GB YT 4,499 ##代码##.70 ,149.30
15 REDFISH 841 ##代码##.02 .82
16 54 DAS @ .00/DAY = 432.00 NaN NaN None
回答by piRSquared
Use iloc
and list indexing
使用iloc
和列出索引
回答by Scott Boston
Use drop with the columns index:
将 drop 与列索引一起使用:
##代码##Output:
输出:
##代码##回答by Vivek Ananthan
If you want to drop the last column
如果你想删除最后一列
##代码##if particular column needs to be dropped with index
如果需要删除特定列和索引
##代码##with column name
带列名
##代码##Hope it helps !
希望能帮助到你 !
回答by Akshay Vilas Patil
fish_frame.drop(fish_frame.columns[len(fish_frame.columns)-1], axis=1, inplace=True)
fish_frame.drop(fish_frame.columns[len(fish_frame.columns)-1], axis=1, inplace=True)