master
/ 8.5.1 pandas文件读写.ipynb

8.5.1 pandas文件读写.ipynb @masterview markup · raw · history · blame

Notebook

Pandas导入外部数据

pandas支持方便快速的从多种格式的外部文件中读取数据形成DataFrame,或将DataFrame写入不同格式的外部文件。 下表是Pandas官方手册上给出的一张表格,表格描述的是Pandas中对各种数据文件类型的读、写函数。

Format Type Data Description Reader Writer
text CSV read_csv to_csv
text Fixed-Width Text File read_fwf
text JSON read_json to_json
text HTML read_html to_html
text LaTeX Styler.to_latex
text XML read_xml to_xml
text Local clipboard read_clipboard to_clipboard
binary MS Excel read_excel to_excel
binary OpenDocument read_excel
binary HDF5 Format read_hdf to_hdf
binary Feather Format read_feather to_feather
binary Parquet Format read_parquet to_parquet
binary ORC Format read_orc
binary Stata read_stata to_stata
binary SAS read_sas
binary SPSS read_spss
binary Python Pickle Format read_pickle to_pickle
SQL SQL read_sql to_sql
SQL Google BigQuery read_gbq to_gbq

本节仅介绍几种常用文件的读取方式,其他方法大家如需使用可自行查询相关文档

读取txt或csv文件

pandas.read_csv(filepath_or_buffer, sep=NoDefault.no_default, delimiter=None, header='infer', names=NoDefault.no_default, index_col=None, usecols=None, squeeze=None, prefix=NoDefault.no_default, mangle_dupe_cols=True, dtype=None, engine=None, converters=None, true_values=None, false_values=None, skipinitialspace=False, skiprows=None, skipfooter=0, nrows=None, na_values=None, keep_default_na=True, na_filter=True, verbose=False, skip_blank_lines=True, parse_dates=None, infer_datetime_format=False, keep_date_col=False, date_parser=None, dayfirst=False, cache_dates=True, iterator=False, chunksize=None, compression='infer', thousands=None, decimal='.', lineterminator=None, quotechar='"', quoting=0, doublequote=True, escapechar=None, comment=None, encoding=None, encoding_errors='strict', dialect=None, error_bad_lines=None, warn_bad_lines=None, on_bad_lines=None, delim_whitespace=False, low_memory=True, memory_map=False, float_precision=None, storage_options=None)

该方法参数较多,下面简单介绍一些常用参数,其他参数含义可自行查询文档。

  • filepath_or_buffer:读取的文件路径,URL(包含http,ftp,s3)链接等
  • sep:指定分隔符,默认逗号分隔。
  • delimiter:定界符,备选分隔符(如果指定该参数,则sep参数失效)
  • delim_whitespace:boolean, 是否指定空白字符作为分隔符,如果为Ture那么delimiter参数失效。
  • header:指定作为整个数据集列名的行,默认为第一行.如果数据集中没有列名,则需要设置为None
  • names:用于结果的列名列表。如果数据文件中没有列标题行,就需要设置header=None
  • index_col:指定数据集中的某列作为行索引
  • usecols:指定只读取文件中的某几列数据
  • dtype:设置每列数据的数据类型
  • skiprows:设置需要忽略的行数(从文件开始处算起),或需要跳过的行号列表(从0开始)
  • skipfooter:设置需要忽略的行数(从文件尾部处算起)
  • nrows:设置需要读取的行数(从文件头开始算起)
  • skip_blank_lines:如果为True,则跳过空行;否则记为NaN

本节结合如下csv文件,仅讲解最简单用法,其他参数大家可结合数据文件自行尝试其效果。

In [ ]:
import pandas as pd


wine_reviews = pd.read_csv("images/ch8/wine point.csv")

print(wine_reviews.shape)   
pd.set_option('display.max_columns', None)   # 显示所有列
pd.set_option('display.max_rows', None)      # 显示所有行
pd.set_option('display.width', None)         # 显示宽度是无限
print(wine_reviews)                          # 返回DataFrame
In [ ]:
# 明确规定某列做行索引
wine_reviews = pd.read_csv("images/ch8/wine point.csv", index_col=6)
print(wine_reviews)                          # 返回DataFrame

读取excel文件

pandas.read_excel(io, sheet_name=0, header=0, names=None, index_col=None, usecols=None, squeeze=None, dtype=None, engine=None, converters=None, true_values=None, false_values=None, skiprows=None, nrows=None, na_values=None, keep_default_na=True, na_filter=True, verbose=False, parse_dates=False, date_parser=None, thousands=None, decimal='.', comment=None, skipfooter=0, convert_float=None, mangle_dupe_cols=True, storage_options=None)

该函数大部分参数与read_csv()方法相同。其中sheet_name用于指定要读取的sheet,默认读取Excel文件中的第一个sheet。 需要注意的是,使用该方法读取.xlsx文件需要借助openpyxl模块,读取.xls文件需要借助xlrd模块。

本节结合文件“2020各手机参数对比.xls”,仅讲解最简单用法,其他参数大家可结合数据文件自行尝试其效果。

In [ ]:
import pandas as pd

phone_infos = pd.read_excel("images/ch8/2020各手机参数对比.xls", skiprows=2) # 跳过第一行的表格标题
# 读取/data/bigfiles/wine point.csv
print(phone_infos.shape)   
pd.set_option('display.max_columns', None)   # 显示所有列
pd.set_option('display.max_rows', None)      # 显示所有行
pd.set_option('display.width', None)         # 显示宽度是无限
pd.set_option('display.unicode.east_asian_width', True) # 显示时列对齐
print(phone_infos)                          # 返回DataFrame

读取HTML网页

pandas.read_html(io, match='.+', flavor=None, header=None, index_col=None, skiprows=None, attrs=None, parse_dates=False, thousands=',', encoding=None, decimal='.', converters=None, na_values=None, keep_default_na=True, displayed_only=True)

该函数是将HTML的表格转换为DataFrame的一种快速方便的方法,不需要用爬虫获取站点的HTML。match参数通过正则表达式匹配需要的表格;flavor参数设置解析器,默认为lxml。

本节以ESPN网站的中超排名表为例,讲解其基本用法。

In [ ]:
pip install lxml
In [ ]:
pip install html5lib
In [8]:
pip install BeautifulSoup4
Looking in indexes: https://mirrors.aliyun.com/pypi/simple
Collecting BeautifulSoup4
  Downloading https://mirrors.aliyun.com/pypi/packages/b1/fe/e8c672695b37eecc5cbf43e1d0638d88d66ba3a44c4d321c796f4e59167f/beautifulsoup4-4.12.3-py3-none-any.whl (147 kB)
     |████████████████████████████████| 147 kB 4.3 MB/s eta 0:00:01
Collecting soupsieve>1.2
  Downloading https://mirrors.aliyun.com/pypi/packages/49/37/673d6490efc51ec46d198c75903d99de59baffdd47aea3d071b80a9e4e89/soupsieve-2.4.1-py3-none-any.whl (36 kB)
Installing collected packages: soupsieve, BeautifulSoup4
Successfully installed BeautifulSoup4-4.12.3 soupsieve-2.4.1
WARNING: You are using pip version 21.1.3; however, version 24.0 is available.
You should consider upgrading via the '/home/jovyan/work/.localenv/bin/python -m pip install --upgrade pip' command.
Note: you may need to restart the kernel to use updated packages.
In [1]:
import pandas as pd
pd.set_option('display.max_columns', None)   # 显示所有列
pd.set_option('display.max_rows', None)      # 显示所有行
pd.set_option('display.width', None)         # 显示宽度是无限
pd.set_option('display.unicode.east_asian_width', True) # 显示时列对齐
# 函数会将每个table转化为一个DataFrame,返回由两个DataFrame构成的列表
CSL_2022 = pd.read_html('https://www.taobao.com/')  # 该页面只有1个table 
print(type(CSL_2022), type(CSL_2022[0]))
print(CSL_2022[0])
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
/tmp/ipykernel_149/3569134230.py in <module>
      5 pd.set_option('display.unicode.east_asian_width', True) # 显示时列对齐
      6 # 函数会将每个table转化为一个DataFrame,返回由两个DataFrame构成的列表
----> 7 CSL_2022 = pd.read_html('https://www.taobao.com/')  # 该页面只有1个table
      8 print(type(CSL_2022), type(CSL_2022[0]))
      9 print(CSL_2022[0])

~/.virtualenvs/basenv/lib/python3.7/site-packages/pandas/util/_decorators.py in wrapper(*args, **kwargs)
    309                     stacklevel=stacklevel,
    310                 )
--> 311             return func(*args, **kwargs)
    312 
    313         return wrapper

~/.virtualenvs/basenv/lib/python3.7/site-packages/pandas/io/html.py in read_html(io, match, flavor, header, index_col, skiprows, attrs, parse_dates, thousands, encoding, decimal, converters, na_values, keep_default_na, displayed_only)
   1111         na_values=na_values,
   1112         keep_default_na=keep_default_na,
-> 1113         displayed_only=displayed_only,
   1114     )

~/.virtualenvs/basenv/lib/python3.7/site-packages/pandas/io/html.py in _parse(flavor, io, match, attrs, encoding, displayed_only, **kwargs)
    924     else:
    925         assert retained is not None  # for mypy
--> 926         raise retained
    927 
    928     ret = []

~/.virtualenvs/basenv/lib/python3.7/site-packages/pandas/io/html.py in _parse(flavor, io, match, attrs, encoding, displayed_only, **kwargs)
    904 
    905         try:
--> 906             tables = p.parse_tables()
    907         except ValueError as caught:
    908             # if `io` is an io-like object, check if it's seekable

~/.virtualenvs/basenv/lib/python3.7/site-packages/pandas/io/html.py in parse_tables(self)
    220         list of parsed (header, body, footer) tuples from tables.
    221         """
--> 222         tables = self._parse_tables(self._build_doc(), self.match, self.attrs)
    223         return (self._parse_thead_tbody_tfoot(table) for table in tables)
    224 

~/.virtualenvs/basenv/lib/python3.7/site-packages/pandas/io/html.py in _parse_tables(self, doc, match, attrs)
    550 
    551         if not tables:
--> 552             raise ValueError("No tables found")
    553 
    554         result = []

ValueError: No tables found
In [ ]:
 
In [ ]: