pandasdataframe读取xlsx⽂件
refer to:
dframe = pd.read_excel(“file_name.xlsx”)
dframe = pd.read_excel(“file_name.xlsx”, sheetname=”Sheet_name”)
dframe = pd.read_excel(“file_name.xlsx”, sheetname=number)
原⽂如下:
//////////////////////////////////////////////////////////////////////////////
Reading and writingExcel files in Python pandas
In data science, you are very likely to mostly work with CSV files. However, knowing how to import and export Excel files is also very useful.
Reading Excel files
dframe = pd.read_excel(“file_name.xlsx”)
Reading Excel files is very similar to reading CSV files. By default, the first sheet of the Excel file is read.
I’ve read an Excel file and viewed the first 5 rows
dframe = pd.read_excel(“file_name.xlsx”, sheetname=”Sheet_name”)
Passing the sheetname method allows you to read the sheet of the Excel file that you want. It is very handy if you know its name.
I picked the sheet named “DonaldTrump”
dframe = pd.read_excel(“file_name.xlsx”, sheetname=number)
If you aren’t sure what are the names of your sheets, you can pick them by their order. Please note that the sheets start from 0 (similar to indices in pandas), not from 1.
I read the second sheet of the Excel file
dframe = pd.read_excel(“file_name.xlsx”, header=None)
Sometimes, the top row does not contain the column names. In this case, you pass the argument of header=None.
The first row is not the header — instead, we get the column names as numbers
dframe = pd.read_excel(“file_name.xlsx”, header=n)
Passing the argument of header being equal to a number allows us to pick a specific row as the column names.
I pick the second row (i.e. row index 1 of the original dataset) as my column names.
dframe = pd.read_excel(“file_name.xlsx”, index_col=number)
python怎么读取xls文件You can use different columns for the row labels by passing the index_col argument as number.
I now use the county as the index column.
dframe = pd.read_excel(“file_name.xlsx”, skiprows=n)
Sometimes, you don’t want to include all of the rows. If you want to skip the first n rows, just pass the
argument of skiprows=n.
Skipping the first two rows (including the header)
Writing an Excel file
<_excel(‘file_name.xlsx’)
I wrote an Excel file called results.xlsx from my results DataFrame
My exported Excel file
<_excel(‘file_name.xlsx’, index=False)
If you don’t want to include the index name (for example, here it is a number so it may be meaningless for future use/analysis), you can just pass another argument, setting index as False.
I don’t want index names in my Excel file
Excel file output with no index names
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论