首页 > 解决方案 > 两列的 Pandas Drop 功能不起作用

问题描述

我正在尝试使用 Pandas Drop 函数删除两列。但是,我收到错误。仅供参考,我已经打印了数据框的列名。为什么我会收到这样的错误?

在 [284] 中:Fulldf.columns

Out[284]:
Index(['PID', 'YearBuilt', 'YearRemodel', 'VeneerExterior', 'BsmtFinTp',
       'BsmtFinSqft', 'BsmtUnfinSqft', 'HeatingQC', 'FstFlrSqft', 'SecFlrSqft',
       'AbvGrndLiving', 'FullBathBsmt', 'HalfBathHouse', 'FullBathHouse',
       'BdrmAbvGrnd', 'RmAbvGrnd', 'Fireplaces', 'GarageTp', 'GarageCars',
       'GarageArea', 'WdDckSqft', 'OpenPrchSqft', 'LotArea', 'LotShape',
       'BldgTp', 'OverallQuality', 'OverallCondition', 'SalePrice'],
      dtype='object')

print(f'Total number of input variables to preprocess: {Fulldf.drop(['SalePrice', 'PID'], axis=1).shape[1]})
**strong text**
In [285]: 
  File "<ipython-input-287-7da1b9aca26a>", line 1
    print(f'Total number of input variables to preprocess: {Fulldf.drop(['SalePrice', 'PID'], axis=1).shape[1]})
                                                                                  ^
SyntaxError: invalid syntax

标签: pythonpandasdataframedata-analysisdrop

解决方案


您正在混合使用单引号和双引号。你能试试这个吗?

print(f"Total number of input variables to preprocess: {Fulldf.drop(['SalePrice', 'PID'], axis=1).shape[1]}")

推荐阅读