[英]What's the most efficient way to find intersections from secondary tables based on a pair of columns in a pandas DataFrame?
[英]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_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.