refer to:

https://medium.com/@kasiarachuta/reading-and-writingexcel-files-in-python-pandas-8f0da449cc48

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 (https://www.kaggle.com/benhamner/d/benhamner/2016-us-election/primary-results-sample-data/output). 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://github.com/kasiarachuta/Blog/blob/master/Reading%20and%20writing%20Excel%20files.ipynb

pandas dataframe 读取 xlsx 文件的更多相关文章

  1. pandas-19 DataFrame读取写入文件的方法

    pandas-19 DataFrame读取写入文件的方法 DataFrame有非常丰富的IO方法,比如DataFrame读写csv文件excel文件等等,操作很简单.下面在代码中标记出来一些常用的读写 ...

  2. 人工智能-机器学习之seaborn(读取xlsx文件,小提琴图)

    我们不止可以读取数据库的内容,还可以读取xlsx文件的内容,这个库有在有些情况还是挺实用的 首先我们想读取这个文件的时候必须得现有个seaborn库 下载命令就是: pip install  seab ...

  3. Python读取xlsx文件

    Python读取xlsx文件 脚本如下: from openpyxl import load_workbook workbook = load_workbook(u'/tmp/test.xlsx') ...

  4. 读取xlsx文件的内容输入到xls文件中

    package com.cn.peitest.excel; import java.io.File; import java.io.FileInputStream; import java.io.Fi ...

  5. C#读取xlsx文件Excel2007

    读取Excel 2007的xlsx文件和读取老的.xls文件是一样的,都是用Oledb读取,仅仅连接字符串不同而已. 具体代码实例: public static DataTable GetExcelT ...

  6. C#基础知识之读取xlsx文件Excel2007

    读取Excel 2007的xlsx文件和读取老的.xls文件是一样的,都是用Oledb读取,仅仅连接字符串不同而已. 具体代码实例: public static DataTable GetExcelT ...

  7. 使用POI读取xlsx文件,包含对excel中自定义时间格式的处理

    package poi; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundExcepti ...

  8. pandas read_csv读取大文件的Memory error问题

    今天在读取一个超大csv文件的时候,遇到困难:首先使用office打不开然后在python中使用基本的pandas.read_csv打开文件时:MemoryError 最后查阅read_csv文档发现 ...

  9. Pandas dataframe数据写入文件和数据库

    转自:http://www.dcharm.com/?p=584 Pandas是Python下一个开源数据分析的库,它提供的数据结构DataFrame极大的简化了数据分析过程中一些繁琐操作,DataFr ...

随机推荐

  1. 使用ffmpeg进行网络直播

    一.采集:使用python调用摄像头采集,原设想是使用树莓派摄像头采集,但是经费紧张买不起,先用摄像头凑合下,反正很简单.                   原理就是先录一小段视频,然后循环播放,用 ...

  2. java第五天

    p37: 练习1 /** * Created by xkfx on 2017/2/22. */ public class DataOnly { int anInt; char aChar; publi ...

  3. 前端学习笔记之CSS后代选择器、子元素选择器、相邻兄弟选择器区别与详解

    派生选择器用的很多,派生选择器具体包括为后代选择器.子元素选择器.相邻兄弟选择器,我们来理解一下他们之间的具体用法与区别. 1.css后代选择器语法:h1 em {color:red;} 表示的是从h ...

  4. slf4j log4j logback相关用法

    Java的简单日志门面( Simple Logging Facade for Java SLF4J)作为一个简单的门面或抽象,用来服务于各种各样的日志框架,比如java.util.logging.lo ...

  5. HDU 6156 Palindrome Function(数位DP)题解

    思路: 数位dp的操作是dfs+记忆化,我们dp开四维:位置,长度,进制,是否回文.然后每次暴搜记录下每个位置的数字是什么,搜到对称轴另一边需要检查是否符合回文. 终于把友谊赛的题目都补完了...没做 ...

  6. ISSCC 2017论文导读 Session 14: A 28nm SoC with a 1.2GHz Prediction Sparse Deep-Neural-Network Engine

    A 28nm SoC with a 1.2GHz 568nJ/Prediction Sparse Deep-Neural-Network Engine with >0.1 Timing Erro ...

  7. VS2012 QT程序打包部署详解

    1.设置安装程序集 InstallShield安装完成后,执行以下步骤右键解决方案-->添加-->新建项目-->其他项目类型-->安装和部署,具体操作如下图: 2.发布程序 在 ...

  8. FreeSouth的学习osg小贴士

    http://www.osgchina.org/index.php?option=com_content&view=article&id=150&catid=91&It ...

  9. Android-----购物车(包含侧滑删除,商品筛选,商品增加和减少,价格计算,店铺分类等)

    电商项目中常常有购物车这个功能,做个很多项目了,都有不同的界面,选了一个来讲一下. 主要包含了 店铺分类,侧滑删除,商品筛选,增加和减少,价格计算等功能. 看看效果图: 重要代码: private v ...

  10. hdu3032sg打表找规律

    先打个表冷静一下 #include<map> #include<set> #include<cmath> #include<queue> #includ ...