如何删除从pandas的Excel中读取的重复列
excel中的数据:
abad 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 ab 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中读取数据时,会自动将第二个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]]
你需要传递axis=1
的drop
:
In [100]: df.drop('a.1', axis=1) Out[100]: abd 0 1 2 4 1 2 3 5 2 3 4 6 3 4 5 7
或者只是通过列select感兴趣的列表:
In [102]: cols = ['a','b','d'] df[cols] Out[102]: abd 0 1 2 4 1 2 3 5 2 3 4 6 3 4 5 7
也适用于“花式索引”:
In [103]: df.ix[:,cols] Out[103]: abd 0 1 2 4 1 2 3 5 2 3 4 6 3 4 5 7