Python 如何在pandas中删除从excel读取的重复列
excel中的数据:Python 如何在pandas中删除从excel读取的重复列,python,excel,python-3.x,pandas,Python,Excel,Python 3.x,Pandas,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从e
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中读取数据时,它会自动将第2列a的列名更改为a.1
我尝试了df.drop(“a.1”,index=1)
,但这不起作用
我有一个巨大的excel文件,它有重复的名称,我只对几个列感兴趣 如果您知道要删除的列的名称:
df = df[[col for col in df.columns if col != 'a.1']]
columns_to_drop = ['a.1', 'b.1', ... ]
df = df[[col for col in df.columns if col not in columns_to_drop]]
如果您有多个要删除的列:
df = df[[col for col in df.columns if col != 'a.1']]
columns_to_drop = ['a.1', 'b.1', ... ]
df = df[[col for col in df.columns if col not in columns_to_drop]]
您需要通过轴=1才能工作: 或者只需传递感兴趣的列列表以进行列选择:
In [102]:
cols = ['a','b','d']
df[cols]
Out[102]:
a b d
0 1 2 4
1 2 3 5
2 3 4 6
3 4 5 7
也适用于“花式索引”:
In [103]:
df.ix[:,cols]
Out[103]:
a b d
0 1 2 4
1 2 3 5
2 3 4 6
3 4 5 7
如果其中一个答案解决了您的问题,请接受它,答案左上角将有一个空勾号(您只能接受一个),这样问题就不会一直没有答案