如何使用iPython中的 pandas 库阅读.xlsx文件? [英] How to read a .xlsx file using the pandas Library in iPython?

查看:672
本文介绍了如何使用iPython中的 pandas 库阅读.xlsx文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用python的Pandas Library读取一个.xlsx文件,并将数据传输到postgreSQL表。



我现在可以做的只有:_

  import pandas as pd 
data = pd.ExcelFile(* File Name *)


现在我知道该步骤已成功执行,但是我想知道如何解析已读取的excel文件,以便我可以了解excel中的数据如何映射到变量数据中的数据。

如果我没有错,我了解到数据是一个Dataframe对象。那么我如何解析这个数据框对象逐行提取每一行。

解决方案

我通常创建一个包含 DataFrame 为每张表:

  xl_file = pd.ExcelFile(file_name)

dfs = {sheet_name:xl_file.parse(sheet_name)
在xl_file.sheet_names中的sheet_name






更新:在熊猫版本0.20.0+(编辑:也许0.19.2),你会得到这个行为更干净地传递 sheetname = None to read_excel

  dfs = pd.read_excel(file_name,sheetname = None)
/ pre>

I want to read a .xlsx file using the Pandas Library of python and port the data to a postgreSQL table.

All I could do up until now is:

import pandas as pd
data = pd.ExcelFile("*File Name*")

Now I know that the step got executed successfully, but I want to know how i can parse the excel file that has been read so that I can understand how the data in the excel maps to the data in the variable data.
I learnt that data is a Dataframe object if I'm not wrong. So How do i parse this dataframe object to extract each line row by row.

解决方案

I usually create a dictionary containing a DataFrame for every sheet:

xl_file = pd.ExcelFile(file_name)

dfs = {sheet_name: xl_file.parse(sheet_name) 
          for sheet_name in xl_file.sheet_names}


Update: In pandas version 0.20.0+ (edit: perhaps 0.19.2 as well) you will get this behavior more cleanly by passing sheetname=None to read_excel:

dfs = pd.read_excel(file_name, sheetname=None)

这篇关于如何使用iPython中的 pandas 库阅读.xlsx文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆