Pandas 处理 Excel 常用方法技巧 #40
shenweiyan
announced in
1.2-编程
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
在使用 Pandas 前需要学习了解一下 Series 和 DataFrame 的基本数据结构和索引的相关概念,之后就可以练习基本的 Excel操作。Pandas 读取一个 Excel 文件后会将其转化为 DataFrame 对象,每一列或行就是一个 Series 对象。这里我们看下如何对一个 excel 进行读写,以及 Sheet、行列、表头处理的一些常用技巧。
读取 Excel
如果您想读取 Excel 表格中的数据,可以使用
read_excel()
方法,其语法格式如下:下表对常用参数做了说明:
sheet_name=None
,才能读取出所有的 sheet,否则默认读取第一个 sheet)。示例如下所示:
获取行数和列数
获取表头
read_excel
默认是把 excel 的第一行当成表头。注意:如果read_excel
的sheet_name=None
,读取的是所有 excel 的 sheet_name(key) 和 sheet_values(values) 组成的字典,df.keys()
的结果是所有 sheet_name,即名字(字典的键)。获取第一个 sheet
这时候
df.keys()
和df.columns
的结果是一样的,都是第一个 sheet 的表头。获取所有 sheet
参考资料
Beta Was this translation helpful? Give feedback.
All reactions