如何使用 iPython 中的 Pandas 库读取 .xlsx 文件? [英] How to read a .xlsx file using the pandas Library in iPython?
问题描述
我想使用 python 的 Pandas 库读取 .xlsx 文件并将数据移植到 postgreSQL 表.
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*")
现在我知道步骤执行成功了,但是我想知道如何解析已读取的excel文件,以便了解excel中的数据如何映射到变量data中的数据.
如果我没记错的话,我了解到 data 是一个 Dataframe 对象.那么我如何解析这个数据框对象以逐行提取每一行.
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.
推荐答案
我通常会为每个工作表创建一个包含 DataFrame
的字典:
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}
<小时>
更新:在 Pandas 0.21.0+ 版本中,您将通过传递 sheet_name=None
到 read_excel
:
dfs = pd.read_excel(file_name, sheet_name=None)
<小时>
在 0.20 及更早版本中,这是 sheetname
而不是 sheet_name
(现在已弃用以支持上述内容):
In 0.20 and prior, this was sheetname
rather than sheet_name
(this is now deprecated in favor of the above):
dfs = pd.read_excel(file_name, sheetname=None)
这篇关于如何使用 iPython 中的 Pandas 库读取 .xlsx 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!