Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/358.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在pandas中删除从excel读取的重复列_Python_Excel_Python 3.x_Pandas - Fatal编程技术网

Python 如何在pandas中删除从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

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中读取数据时,它会自动将第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

如果其中一个答案解决了您的问题,请接受它,答案左上角将有一个空勾号(您只能接受一个),这样问题就不会一直没有答案