pandas 删除列
要删除Pandas数据帧中的列,可以使用drop()方法。以下是删除列的几种常见方法:
- 使用列名称删除单个列:
df.drop('column_name', axis=1, inplace=True)
- 使用列名称删除多个列:
df.drop(['column_name1', 'column_name2'], axis=1, inplace=True)
- 使用列索引删除单个列:
df.drop(df.columns[index], axis=1, inplace=True)
- 使用列索引删除多个列:
df.drop(df.columns[[index1, index2]], axis=1, inplace=True)
请注意,drop()方法默认返回一个新的数据帧,如果要在原始数据帧上进行修改,需要设置inplace=True参数。
原文地址: https://www.cveoy.top/t/topic/hysa 著作权归作者所有。请勿转载和采集!