博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
pandas dataframe 读取 xlsx 文件
阅读量:7049 次
发布时间:2019-06-28

本文共 2879 字,大约阅读时间需要 9 分钟。

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.

In this post, a Kaggle dataset on 2016 US Elections was used (). This dataset has been converted from a CSV file to an Excel file and two sheets have been added with votes for Hilary Clinton (HilaryClinton) and Donald Trump (DonaldTrump). The first sheet (All) contains the original dataset.

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)

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

dframe.to_excel(‘file_name.xlsx’)

 

I wrote an Excel file called results.xlsx from my results DataFrame

 

My exported Excel file

dframe.to_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

All of the code can be found on my GitHub: 

 

 

 

 

 

转载于:https://www.cnblogs.com/qingyuanjushi/p/6736810.html

你可能感兴趣的文章
ORACLE里锁有以下几种模式,v$locked_object,locked_mode
查看>>
【树莓派】Linux 测网速及树莓派源
查看>>
Java用户线程和守护线程
查看>>
[TypeScript] Use the never type to avoid code with dead ends using TypeScript
查看>>
Javascript 与 SPA单页Web富应用
查看>>
SpringMVC之访问静态文件
查看>>
【java设计模式】之 模板方法(Template Method)模式
查看>>
【踩坑速记】MIUI系统BUG,调用系统相机拍照可能会带给你的一系列坑,将拍照适配方案进行到底!...
查看>>
小米手机会不会更好
查看>>
atitit.Sealink2000国际海运信息管理系统
查看>>
android面试总结01 activity生命周期
查看>>
Java 实现策略(Strategy)模式
查看>>
Python文本爬虫实战
查看>>
leetcode:Gray Code
查看>>
IDEA+PHP+XDebug调试配置
查看>>
Jenkins
查看>>
Ubuntu离线安装Sogou拼音(附老版本安装&输入法自启动)
查看>>
springmvc结合base64存取图片到mysql
查看>>
深度学习主机环境配置: Ubuntu16.04+GeForce GTX 1080+TensorFlow
查看>>
linux 抓包 tcpdump 简单应用
查看>>