python dataframe pandas drop column using int [英] python dataframe pandas drop column using int

查看:234
本文介绍了python dataframe pandas drop column using int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道要删除一列,你使用df.drop('column name',axis = 1)。有没有办法使用数字索引而不是列名删除列?

解决方案

您可以删除 i 这样的索引:

  df.drop(df.columns [i] ,轴= 1)

如果列中有重复的名称,可能会工作很奇怪,所以要做这可以通过新名称重命名要删除列的列。或者您可以像这样重新分配DataFrame:

  df = df.iloc [:, [j for j,c in enumerate(df如果j!= i]] 


I understand that to drop a column you use df.drop('column name', axis=1). Is there a way to drop a column using a numerical index instead of the column name?

解决方案

You can delete column on i index like this:

df.drop(df.columns[i], axis=1)

It could work strange, if you have duplicate names in columns, so to do this you can rename column you want to delete column by new name. Or you can reassign DataFrame like this:

df = df.iloc[:, [j for j, c in enumerate(df.columns) if j != i]]

这篇关于python dataframe pandas drop column using int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆