Skip specific set of columns when reading excel fr

2019-07-21 21:53发布

I know beforehand what columns I don't need from an excel file and I'd like to avoid them when reading the file to improve the performance. Something like this:

import pandas as pd
df = pd.read_excel('large_excel_file.xlsx', skip_cols=['col_a', 'col_b',...,'col_zz'])

There is nothing related to this in the documentation. is there any workaround for this?

2条回答
不美不萌又怎样
2楼-- · 2019-07-21 22:45

If your version of pandas allows (check first if you can pass a function to usecols), I would try something like:

import pandas as pd
df = pd.read_excel('large_excel_file.xlsx', usecols=lambda x: 'Unnamed' not in x,)

This should skip all columns without header names. You could substitute 'Unnamed' with a list of column names you do not want.

查看更多
爱情/是我丢掉的垃圾
3楼-- · 2019-07-21 22:57

You can use the following technique:

In [7]: cols2skip = [2,5,8]

In [8]: cols = [i for i in range(10) if i not in cols2skip]

In [9]: cols
Out[9]: [0, 1, 3, 4, 6, 7, 9]

and then

df = pd.read_excel(filename, usecols=cols)
查看更多
登录 后发表回答