How do I exclude header and footer from an excel file in pandas

血红的双手。 提交于 2021-02-11 06:00:53

问题


df=pd.read_excel(filename)

What attributes should I add to exclude header and footer?


回答1:


Head over here: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.read_excel.html

you will find

pandas.read_excel(io, sheet_name=0, header=0, names=None, index_col=None, usecols=None, squeeze=False, dtype=None, engine=None, converters=None, true_values=None, false_values=None, skiprows=None, nrows=None, na_values=None, keep_default_na=True, verbose=False, parse_dates=False, date_parser=None, thousands=None, comment=None, skipfooter=0, convert_float=True, mangle_dupe_cols=True, **kwds)

Out of which ones which are useful to you are:

pandas.read_excel(skiprows=None, skipfooter=0)

you can specify the value in integer to skiprows=1 to skip header and skipfooter=1 to skip footer you can add as many rows are you want to skip

Hope it helps




回答2:


This is how I achieved it:

energy = pd.read_excel('Energy Indicators.xls', index_col=None, header=None, footer=None)
energy = energy[18:245].reset_index()

I used this for data cleaning in my assignment where I was required to remove header and footer.

So I first imported raw data from excel using

energy = pd.read_excel('Energy Indicators.xls', index_col=None, header=None, footer=None)

Now since the row from which actual required data started is 18 and row where footer started is 245 I used this

energy = energy[18:245].reset_index()

I used reset_index() because after removing rows my index was messed up so to reset it.

You might want to remove that extra column named 'index' which is created by reset_index()




回答3:


you should create a sun dataframe from previous data frame which doesn't contain the header and footer attribute.

for example,

if you excel file contains 500 rows, in which first three rows are header and last 10 rows are footers then you can do as below:

df = df[4:491]



来源:https://stackoverflow.com/questions/61004648/how-do-i-exclude-header-and-footer-from-an-excel-file-in-pandas

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!