使用del和drop方法刪除DataFrame中的列,使用drop方法一次刪除多列
# 使用del, 一次只能刪除一列,不能一次刪除多列
# 只能使用 del df['密度'], 不能使用 del df[['密度', '含糖率']]
del df['密度']
# del df[['密度', '含糖率']] 報錯
# 使用drop,有三種方法:
dt = dt.drop(['密度', '含糖率'], axis=1) # axis=1 表示刪除列,['密度', '含糖率'] 要刪除的col的列表,可一次刪除多列
dt.drop(['密度', '含糖率'], axis=1, inplace=True) # inplace=True, 直接從內部刪除
dt.drop(dt.columns[[0, 4, 8]], axis=1, inplace=True) # dt.columns[[0, 4, 8]] 直接使用索引查找列