如何删除从 pandas 中的excel读取的重复列 [英] how to delete a duplicate column read from excel in pandas

查看:36
本文介绍了如何删除从 pandas 中的excel读取的重复列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Excel中的数据:

Data in excel:

a   b   a   d
1   2   3   4
2   3   4   5
3   4   5   6
4   5   6   7

代码:

df= pd.io.excel.read_excel(r"sample.xlsx",sheetname="Sheet1")
df
   a  b  a.1  d
0  1  2    3  4
1  2  3    4  5
2  3  4    5  6
3  4  5    6  7

如何删除 a.1 列?

当pandas从excel读取数据时,它会自动将2nd a的列名更改为a.1.

when pandas reads the data from excel it automatically changes the column name of 2nd a to a.1.

我尝试了 df.drop("a.1",index = 1),但这不起作用.

I tried df.drop("a.1",index=1) , this does not work.

我有一个巨大的excel文件,其中包含重复的名称,并且我只对少数几列感兴趣.

I have a huge excel file which has duplicate names, and i am interested only in few of columns.

推荐答案

如果知道要删除的列的名称:

If you know the name of the column you want to drop:

df = df[[col for col in df.columns if col != 'a.1']]

,如果您有几列,则要删除:

and if you have several columns you want to drop:

columns_to_drop = ['a.1', 'b.1', ... ]
df = df[[col for col in df.columns if col not in columns_to_drop]]

这篇关于如何删除从 pandas 中的excel读取的重复列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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