pandas中DataFrame的“索引列”,删除pandas中的列
关于DataFrame中的“索引列”
在DataFrame中的索引都在最左边的一列中,暂且叫它“索引列”,但其实它不是一个列,所以适用于删除列的命令都不适合它
删除列
方法一:del df[‘index’]
score=pd.read_csv('./dietplan.txt',sep='\t',index_col='Participant')
score
score=pd.read_csv('./dietplan.txt',sep='\t',index_col='Participant')
del score['Diet-Plan1']
score
但如果想用del删除设定为索引的那一列,就会报错:
score=pd.read_csv('./dietplan.txt',sep='\t',index_col='Participant')
del score['Participant']
score
方法二:df.drop([‘index’],axis = 1)
score=pd.read_csv('./dietplan.txt',sep='\t',index_col='Participant')
score=score.drop(['Diet-Plan1'],axis=1)
score
如果想用drop方法删除设定为索引的那一列,也会报错:
score=pd.read_csv('./dietplan.txt',sep='\t',index_col='Participant')
score=score.drop(['Participant'],axis=1)
score
删除“索引列”
如果DataFrame中的“索引列”是一个有实际意义的属性,例如:
这个表中的“Rank”是索引,但是同时它也代表了这些学校的2018的QS排名。
如果需要删除这一列,那么则需要去除这一列的索引属性,就是不把这一列设为索引。
qs = pd.read_excel('./2018-QS-World-University-Rankings-Top200.xlsx',index=False)
然后再用上面的两种方法进行删除