繁体   English   中英

从大型数据框中删除熊猫中的列(从开始到结束)的最有效方法是什么?

[英]What's the most efficient way to drop columns (from beginning and end) in pandas from a large dataframe?

我正在尝试从pandas数据框的开头和结尾删除许多列。

我的数据框有397行和291列。 我目前有此解决方案来删除前8列,但我也想在结尾处删除一些:

SMPS_Data = SMPS_Data.drop(SMPS_Data.columns[0:8], axis=1)

我知道我可以重复此步骤并删除最后几列,但是我希望有一种更直接的方法来解决此问题。

我尝试使用

SMPS_Data = SMPS_Data.drop(SMPS_Data.columns[0:8,278:291], axis=1)

但这不起作用。

此外,.drop方法似乎以某种方式减慢了控制台的响应速度,所以也许有一种更干净的方法可以做到这一点?

如果.drop()列名称删除列,则可以使用.drop()

drop_these = ['column_name1', 'column_name2', 'last_columns']
df = df.drop(columns=drop_these)

如果您知道要按其位置删除它们,则可以使用.iloc()

df.iloc[:, 8:15]  # For columns 8-15
df.iloc[:, :-5]   # For all columns, except the last five
df.iloc[:. 2:-5]  # For all columns, except the first column, and the last five

有关更多信息,请参见本文档中有关用熊猫建立索引和切片数据的 文档

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM