Python酷库之旅-第三方库Pandas(093)

目录

一、用法精讲

396、pandas.Series.to_frame方法

396-1、语法

396-2、参数

396-3、功能

396-4、返回值

396-5、说明

396-6、用法

396-6-1、数据准备

396-6-2、代码示例

396-6-3、结果输出

397、pandas.Series.to_xarray方法

397-1、语法

397-2、参数

397-3、功能

397-4、返回值

397-5、说明

397-6、用法

397-6-1、数据准备

397-6-2、代码示例

397-6-3、结果输出

398、pandas.Series.to_hdf方法

398-1、语法

398-2、参数

398-3、功能

398-4、返回值

398-5、说明

398-6、用法

398-6-1、数据准备

398-6-2、代码示例

398-6-3、结果输出

399、pandas.Series.to_sql方法

399-1、语法

399-2、参数

399-3、功能

399-4、返回值

399-5、说明

399-6、用法

399-6-1、数据准备

399-6-2、代码示例

399-6-3、结果输出

400、pandas.Series.to_json方法

400-1、语法

400-2、参数

400-3、功能

400-4、返回值

400-5、说明

400-6、用法

400-6-1、数据准备

400-6-2、代码示例

400-6-3、结果输出

二、推荐阅读

1、Python筑基之旅

2、Python函数之旅

3、Python算法之旅

4、Python魔法之旅

5、博客个人主页

一、用法精讲

396、pandas.Series.to_frame方法
396-1、语法
# 396、pandas.Series.to_frame方法
pandas.Series.to_frame(name=_NoDefault.no_default)
Convert Series to DataFrame.

Parameters:
name
object, optional
The passed name should substitute for the series name (if it has one).

Returns:
DataFrame
DataFrame representation of Series.
396-2、参数

396-2-1、name(可选)如果你没有指定name参数,pandas会使用Series自己的名字作为列名;如果Series没有名字,DataFrame中的列名将保持未命名状态。

396-3、功能

        将一个pandas Series转换为单列的DataFrame。

396-4、返回值

        返回一个pandas DataFrame对象,这个DataFrame有一列,其值来自于原来的Series。

396-5、说明

        无

396-6、用法
396-6-1、数据准备
396-6-2、代码示例
# 396、pandas.Series.to_frame方法
# 396-1、使用默认列名(Series自带名字)
import pandas as pd
# 创建一个有名称的Series
s = pd.Series([1, 2, 3], name='my_series')
# 将Series转换为DataFrame
df = s.to_frame()
print(df)

# 396-2、指定列名
import pandas as pd
# 创建一个没有名称的Series
s = pd.Series([1, 2, 3])
# 将Series转换为DataFrame,并指定列名
df = s.to_frame(name='new_column')
print(df)

# 396-3、Series没有名字,且不指定列名
import pandas as pd
# 创建一个没有名称的Series
s = pd.Series([1, 2, 3])
# 将Series转换为DataFrame,不指定列名
df = s.to_frame()
print(df)
396-6-3、结果输出
# 396、pandas.Series.to_frame方法
# 396-1、使用默认列名(Series自带名字)
#    my_series
# 0          1
# 1          2
# 2          3

# 396-2、指定列名
#    new_column
# 0           1
# 1           2
# 2           3

# 396-3、Series没有名字,且不指定列名
#    0
# 0  1
# 1  2
# 2  3
397、pandas.Series.to_xarray方法
397-1、语法
# 397、pandas.Series.to_xarray方法
pandas.Series.to_xarray()
Return an xarray object from the pandas object.

Returns:
xarray.DataArray or xarray.Dataset
Data in the pandas structure converted to Dataset if the object is a DataFrame, or a DataArray if the object is a Series.
397-2、参数

        无

397-3、功能

        将一个Pandas Series对象转换为一个xarray DataArray对象

397-4、返回值

        返回一个xarray.DataArray对象,该对象将包含原Series的数据及其索引。

397-5、说明

        无

397-6、用法
397-6-1、数据准备
397-6-2、代码示例
# 397、pandas.Series.to_xarray方法
# 397-1、基本用法
import pandas as pd
# 导入xarray库
import xarray as xr
# 创建一个Pandas Series对象
s = pd.Series([10, 20, 30], index=['a', 'b', 'c'], name='example_series')
# 将Series转换为DataArray
data_array = s.to_xarray()
print(data_array, end='\n\n')

# 397-2、实例分析
import pandas as pd
# 需要有一个Series对象,假设它包含了一些时间序列数据
dates = pd.date_range('20240101', periods=4)
data = pd.Series([1.1, 2.3, 3.6, 4.0], index=dates, name='data_series')
# 将Series转换为DataArray
data_array = data.to_xarray()
print(data_array)
397-6-3、结果输出
# 397、pandas.Series.to_xarray方法
# 397-1、基本用法
# <xarray.DataArray 'example_series' (index: 3)> Size: 24B
# array([10, 20, 30], dtype=int64)
# Coordinates:
#   * index    (index) object 24B 'a' 'b' 'c'

# 397-2、实例分析
# <xarray.DataArray 'data_series' (index: 4)> Size: 32B
# array([1.1, 2.3, 3.6, 4. ])
# Coordinates:
#   * index    (index) datetime64[ns] 32B 2024-01-01 2024-01-02 ... 2024-01-04
398、pandas.Series.to_hdf方法
398-1、语法
# 398、pandas.Series.to_hdf方法
pandas.Series.to_hdf(path_or_buf, *, key, mode='a', complevel=None, complib=None, append=False, format=None, index=True, min_itemsize=None, nan_rep=None, dropna=None, data_columns=None, errors='strict', encoding='UTF-8')
Write the contained data to an HDF5 file using HDFStore.

Hierarchical Data Format (HDF) is self-describing, allowing an application to interpret the structure and contents of a file with no outside information. One HDF file can hold a mix of related objects which can be accessed as a group or as individual objects.

In order to add another DataFrame or Series to an existing HDF file please use append mode and a different a key.

Warning

One can store a subclass of DataFrame or Series to HDF5, but the type of the subclass is lost upon storing.

For more information see the user guide.

Parameters:
path_or_buf
str or pandas.HDFStore
File path or HDFStore object.

key
str
Identifier for the group in the store.

mode
{‘a’, ‘w’, ‘r+’}, default ‘a’
Mode to open file:

‘w’: write, a new file is created (an existing file with the same name would be deleted).

‘a’: append, an existing file is opened for reading and writing, and if the file does not exist it is created.

‘r+’: similar to ‘a’, but the file must already exist.

complevel
{0-9}, default None
Specifies a compression level for data. A value of 0 or None disables compression.

complib
{‘zlib’, ‘lzo’, ‘bzip2’, ‘blosc’}, default ‘zlib’
Specifies the compression library to be used. These additional compressors for Blosc are supported (default if no compressor specified: ‘blosc:blosclz’): {‘blosc:blosclz’, ‘blosc:lz4’, ‘blosc:lz4hc’, ‘blosc:snappy’, ‘blosc:zlib’, ‘blosc:zstd’}. Specifying a compression library which is not available issues a ValueError.

append
bool, default False
For Table formats, append the input data to the existing.

format
{‘fixed’, ‘table’, None}, default ‘fixed’
Possible values:

‘fixed’: Fixed format. Fast writing/reading. Not-appendable, nor searchable.

‘table’: Table format. Write as a PyTables Table structure which may perform worse but allow more flexible operations like searching / selecting subsets of the data.

If None, pd.get_option(‘io.hdf.default_format’) is checked, followed by fallback to “fixed”.

index
bool, default True
Write DataFrame index as a column.

min_itemsize
dict or int, optional
Map column names to minimum string sizes for columns.

nan_rep
Any, optional
How to represent null values as str. Not allowed with append=True.

dropna
bool, default False, optional
Remove missing values.

data_columns
list of columns or True, optional
List of columns to create as indexed data columns for on-disk queries, or True to use all columns. By default only the axes of the object are indexed. See Query via data columns. for more information. Applicable only to format=’table’.

errors
str, default ‘strict’
Specifies how encoding and decoding errors are to be handled. See the errors argument for open() for a full list of options.

encoding
str, default “UTF-8”.
398-2、参数

398-2-1、path_or_buf(必须)字符串或可类文件对象,指定要保存的HDF5文件的路径,如果文件不存在,将会自动创建一个新的文件;如果提供的是类文件对象,它需要支持write()方法。

398-2-2、key(必须)字符串,指定在HDF5文件中保存数据的节点路径,该路径在文件中将充当数据的唯一标识符,类似于文件系统中的文件路径。

398-2-3、mode(可选,默认值为'a')字符串,指定文件的打开模式,选项有:

  • 'r':只读模式
  • 'w':写模式,覆盖文件
  • 'a':追加模式
  • 'r+': 读写模式

398-2-4、complevel(可选,默认值为None)整数,范围为 0-9,指定压缩级别。级别越高,压缩率越大,但压缩和解压缩的速度也越慢,0表示不压缩。

398-2-5、complib(可选,默认值为None)字符串,指定压缩库,可以选择的值包括'zlib''lzo''bzip2''blosc',压缩库决定了数据的压缩算法。

398-2-6、append(可选,默认值为False)布尔值,如果为True,数据将追加到现有的节点;如果为False,现有节点的数据将被覆盖。

398-2-7、format(可选,默认值为None)字符串,指定数据的存储格式,'fixed'表示固定格式(写入速度快但灵活性低),'table'表示表格式(写入速度慢但更灵活)。

398-2-8、index(可选,默认值为True)布尔值,是否将索引保存到HDF5文件中。

398-2-9、min_itemsize(可选,默认值为None)字典或整数,指定字符串列的最小大小,如果需要存储很长的字符串,可以使用这个参数来指定最小的存储空间。

398-2-10、nan_rep(可选,默认值为None)字符串,用于替换NaN值的字符串表示。

398-2-11、dropna(可选,默认值为None)布尔值,如果为True,将不保存包含NaN的列。

398-2-12、data_columns(可选,默认值为None)列表,指定哪些列应该作为数据列进行索引,这在检索时可以加快查询速度。

398-2-13、errors(可选,默认值为'strict')字符串,指定在编码错误时的处理方式,'strict'会引发错误,'ignore'会忽略错误,'replace'会用替代字符替换错误。

398-2-14、encoding(可选,默认值为'UTF-8')字符串,指定在保存字符串数据时使用的编码。

398-3、功能

        将Pandas Series对象保存到HDF5文件中,并且可以选择性地对数据进行压缩、指定存储格式等。

398-4、返回值

        该方法没有返回值。它直接将数据写入到指定的HDF5文件中。

398-5、说明

        无

398-6、用法
398-6-1、数据准备
398-6-2、代码示例
# 398、pandas.Series.to_hdf方法
import pandas as pd
# 创建一个Series对象
s = pd.Series([1, 2, 3], index=['a', 'b', 'c'])
# 将Series保存到HDF5文件中
s.to_hdf('data.h5', key='series_data', mode='w', format='table')
398-6-3、结果输出

399、pandas.Series.to_sql方法
399-1、语法
# 399、pandas.Series.to_sql方法
pandas.Series.to_sql(name, con, *, schema=None, if_exists='fail', index=True, index_label=None, chunksize=None, dtype=None, method=None)
Write records stored in a DataFrame to a SQL database.

Databases supported by SQLAlchemy [1] are supported. Tables can be newly created, appended to, or overwritten.

Parameters:
namestr
Name of SQL table.

consqlalchemy.engine.(Engine or Connection) or sqlite3.Connection
Using SQLAlchemy makes it possible to use any DB supported by that library. Legacy support is provided for sqlite3.Connection objects. The user is responsible for engine disposal and connection closure for the SQLAlchemy connectable. See here. If passing a sqlalchemy.engine.Connection which is already in a transaction, the transaction will not be committed. If passing a sqlite3.Connection, it will not be possible to roll back the record insertion.

schemastr, optional
Specify the schema (if database flavor supports this). If None, use default schema.

if_exists{‘fail’, ‘replace’, ‘append’}, default ‘fail’
How to behave if the table already exists.

fail: Raise a ValueError.

replace: Drop the table before inserting new values.

append: Insert new values to the existing table.

indexbool, default True
Write DataFrame index as a column. Uses index_label as the column name in the table. Creates a table index for this column.

index_labelstr or sequence, default None
Column label for index column(s). If None is given (default) and index is True, then the index names are used. A sequence should be given if the DataFrame uses MultiIndex.

chunksizeint, optional
Specify the number of rows in each batch to be written at a time. By default, all rows will be written at once.

dtypedict or scalar, optional
Specifying the datatype for columns. If a dictionary is used, the keys should be the column names and the values should be the SQLAlchemy types or strings for the sqlite3 legacy mode. If a scalar is provided, it will be applied to all columns.

method{None, ‘multi’, callable}, optional
Controls the SQL insertion clause used:

None : Uses standard SQL INSERT clause (one per row).

‘multi’: Pass multiple values in a single INSERT clause.

callable with signature (pd_table, conn, keys, data_iter).

Details and a sample callable implementation can be found in the section insert method.

Returns:
None or int
Number of rows affected by to_sql. None is returned if the callable passed into method does not return an integer number of rows.

The number of returned rows affected is the sum of the rowcount attribute of sqlite3.Cursor or SQLAlchemy connectable which may not reflect the exact number of written rows as stipulated in the sqlite3 or SQLAlchemy.

New in version 1.4.0.

Raises:
ValueError
When the table already exists and if_exists is ‘fail’ (the default).
399-2、参数

399-2-1、name(必须)字符串,SQL表的名称,这是将要写入数据的目标表名,如果表名中有特殊字符或关键字,可能需要用引号括起来。

399-2-2、con(必须)一个数据库连接对象,表示要连接的数据库,常用的是SQLAlchemy引擎或SQLite的连接对象。

399-2-3、schema(可选,默认值为None)字符串,指定数据库中的模式(Schema),如果未指定,默认使用数据库的默认模式。

399-2-4、if_exists(可选,默认值为'fail')字符串,当表已存在时,指定如何处理数据的写入:

  • 'fail':引发错误,表示表已存在。
  • 'replace':删除现有表并创建一个新表。
  • 'append':将数据追加到现有表中。

399-2-5、index(可选,默认值为True)布尔值,是否将Series的索引作为列写入到SQL表中,如果为True,索引将作为表的一列进行存储。

399-2-6、index_label(可选,默认值为None)字符串或列表,指定索引列的列名,如果不指定,将使用Series索引的名称;如果Series的索引没有名称,将使用index作为列名。

399-2-7、chunksize(可选,默认值为None)整数,将数据分块写入数据库的每个块的大小,如果数据量很大,这个参数可以帮助避免内存问题。

399-2-8、dtype(可选,默认值为None)字典或None指定列的数据类型。例如{'col_name': sqlalchemy.types.Integer}可以将列的数据类型指定为Integer,这对确保数据在SQL表中的类型一致性非常有用。

399-2-9、method(可选,默认值为None)字符串或可调用对象,指定用于插入数据的SQL语句的执行方法,可以是'multi'(执行多个值的单个SQL语句)或自定义函数来处理数据插入的逻辑,使用'multi'可以加快插入速度。

399-3、功能

        将Pandas Series的数据插入到指定的SQL表中,如果表已经存在,可以选择追加数据或替换表,此方法对处理和分析数据特别有用,尤其是当数据需要长期存储或需要通过SQL进行复杂查询时。

399-4、返回值

        此方法返回写入的行数(int类型),表示成功插入到SQL表中的记录数量。

399-5、说明

        无

399-6、用法
399-6-1、数据准备
399-6-2、代码示例
# 399、pandas.Series.to_sql方法
import pandas as pd
from sqlalchemy import create_engine
# 创建一个Series对象
s = pd.Series([10, 20, 30], index=['a', 'b', 'c'])
# 创建一个数据库连接 (例如, SQLite 数据库)
engine = create_engine('sqlite:///my_database.db')
# 将Series保存到SQL表中
s.to_sql(name='my_table', con=engine, if_exists='replace', index=True)
399-6-3、结果输出

        无

400、pandas.Series.to_json方法
400-1、语法
# 400、pandas.Series.to_json方法
pandas.Series.to_json(path_or_buf=None, *, orient=None, date_format=None, double_precision=10, force_ascii=True, date_unit='ms', default_handler=None, lines=False, compression='infer', index=None, indent=None, storage_options=None, mode='w')
Convert the object to a JSON string.

Note NaN’s and None will be converted to null and datetime objects will be converted to UNIX timestamps.

Parameters:
path_or_bufstr, path object, file-like object, or None, default None
String, path object (implementing os.PathLike[str]), or file-like object implementing a write() function. If None, the result is returned as a string.

orientstr
Indication of expected JSON string format.

Series:

default is ‘index’

allowed values are: {‘split’, ‘records’, ‘index’, ‘table’}.

DataFrame:

default is ‘columns’

allowed values are: {‘split’, ‘records’, ‘index’, ‘columns’, ‘values’, ‘table’}.

The format of the JSON string:

‘split’ : dict like {‘index’ -> [index], ‘columns’ -> [columns], ‘data’ -> [values]}

‘records’ : list like [{column -> value}, … , {column -> value}]

‘index’ : dict like {index -> {column -> value}}

‘columns’ : dict like {column -> {index -> value}}

‘values’ : just the values array

‘table’ : dict like {‘schema’: {schema}, ‘data’: {data}}

Describing the data, where data component is like orient='records'.

date_format{None, ‘epoch’, ‘iso’}
Type of date conversion. ‘epoch’ = epoch milliseconds, ‘iso’ = ISO8601. The default depends on the orient. For orient='table', the default is ‘iso’. For all other orients, the default is ‘epoch’.

double_precisionint, default 10
The number of decimal places to use when encoding floating point values. The possible maximal value is 15. Passing double_precision greater than 15 will raise a ValueError.

force_asciibool, default True
Force encoded string to be ASCII.

date_unitstr, default ‘ms’ (milliseconds)
The time unit to encode to, governs timestamp and ISO8601 precision. One of ‘s’, ‘ms’, ‘us’, ‘ns’ for second, millisecond, microsecond, and nanosecond respectively.

default_handlercallable, default None
Handler to call if object cannot otherwise be converted to a suitable format for JSON. Should receive a single argument which is the object to convert and return a serialisable object.

linesbool, default False
If ‘orient’ is ‘records’ write out line-delimited json format. Will throw ValueError if incorrect ‘orient’ since others are not list-like.

compressionstr or dict, default ‘infer’
For on-the-fly compression of the output data. If ‘infer’ and ‘path_or_buf’ is path-like, then detect compression from the following extensions: ‘.gz’, ‘.bz2’, ‘.zip’, ‘.xz’, ‘.zst’, ‘.tar’, ‘.tar.gz’, ‘.tar.xz’ or ‘.tar.bz2’ (otherwise no compression). Set to None for no compression. Can also be a dict with key 'method' set to one of {'zip', 'gzip', 'bz2', 'zstd', 'xz', 'tar'} and other key-value pairs are forwarded to zipfile.ZipFile, gzip.GzipFile, bz2.BZ2File, zstandard.ZstdCompressor, lzma.LZMAFile or tarfile.TarFile, respectively. As an example, the following could be passed for faster compression and to create a reproducible gzip archive: compression={'method': 'gzip', 'compresslevel': 1, 'mtime': 1}.

New in version 1.5.0: Added support for .tar files.

Changed in version 1.4.0: Zstandard support.

indexbool or None, default None
The index is only used when ‘orient’ is ‘split’, ‘index’, ‘column’, or ‘table’. Of these, ‘index’ and ‘column’ do not support index=False.

indentint, optional
Length of whitespace used to indent each record.

storage_optionsdict, optional
Extra options that make sense for a particular storage connection, e.g. host, port, username, password, etc. For HTTP(S) URLs the key-value pairs are forwarded to urllib.request.Request as header options. For other URLs (e.g. starting with “s3://”, and “gcs://”) the key-value pairs are forwarded to fsspec.open. Please see fsspec and urllib for more details, and for more examples on storage options refer here.

modestr, default ‘w’ (writing)
Specify the IO mode for output when supplying a path_or_buf. Accepted args are ‘w’ (writing) and ‘a’ (append) only. mode=’a’ is only supported when lines is True and orient is ‘records’.

Returns:
None or str
If path_or_buf is None, returns the resulting json format as a string. Otherwise returns None.
400-2、参数

400-2-1、path_or_buf(必须)字符串或文件对象,指定JSON数据的输出路径,如果为None,则返回JSON格式的字符串;否则,将JSON数据写入到指定的文件或缓冲区。

400-2-2、orient(可选,默认值为None)字符串,确定JSON输出格式,可选值有:

  • 'split':输出{"index" -> [index], "data" -> [values]}
  • 'records':输出 [{"index": index, "data": value}, ...]
  • 'index':输出 {index -> value}
  • 'values':输出 [value, ...]
  • 'table':输出JSONTable Schema格式。

400-2-3、date_format(可选,默认值为None)字符串,指定日期格式,可选值为'epoch'(时间戳)或'iso'(ISO8601日期格式),如果为None,则使用默认的Pandas日期处理方式。

400-2-4、double_precision(可选,默认值为10)整数,控制浮点数的精度,表示小数点后的有效位数。

400-2-5、force_ascii(可选,默认值为True)布尔值,如果为True,则所有非ASCII字符将被转义,设置为False可以保留原始Unicode字符。

400-2-6、date_unit(可选,默认值为'ms')字符串,指定时间单位,可选值有's'(秒)、'ms'(毫秒)、'us'(微秒)、'ns'(纳秒)。

400-2-7、default_handler(可选,默认值为None)可调用对象,用于处理无法直接序列化为JSON的对象,可以传递一个自定义函数,以确定如何处理这些对象。

400-2-8、lines(可选,默认值为False)布尔值,如果为True,每一行将作为一条JSON记录输出,这对于处理大文件非常有用。

400-2-9、compression(可选,默认值为'infer')字符串或None,指定压缩方式,可选值包括'gzip'、'bz2'、'zip'、'xz'、'zstd'或None,使用'infer'时,将根据文件扩展名自动推断压缩方式。

400-2-10、index(可选,默认值为None)布尔值,指定是否包含Series的索引,如果为None,则默认为True。

400-2-11、indent(可选,默认值为None)整数,设置JSON输出的缩进级别,None表示没有缩进,JSON将输出为紧凑格式。

400-2-12、storage_options(可选,默认值为None)字典,传递给文件系统的额外存储选项(例如使用S3、GCS的配置参数)。

400-2-13、mode(可选,默认值为'w')字符串,文件的写入模式,常用模式包括'w'(写入)、'a'(追加)。

400-3、功能

        将Series对象序列化为JSON格式,它提供了多种参数,以灵活地控制输出的格式、精度和其他细节,可以选择将结果写入文件或返回为JSON字符串。

400-4、返回值

        返回一个包含Series数据的JSON格式字符串,如果指定了path_or_buf,则将JSON写入文件,并返回None。

400-5、说明

        无

400-6、用法
400-6-1、数据准备
400-6-2、代码示例
# 400、pandas.Series.to_json方法
import pandas as pd
# 创建一个Series对象
s = pd.Series([1, 2, 3], index=['a', 'b', 'c'])
# 将Series转换为JSON字符串
json_str = s.to_json()
# 将Series保存为JSON文件
s.to_json('output.json', orient='split', indent=4)
400-6-3、结果输出

二、推荐阅读

1、Python筑基之旅
2、Python函数之旅
3、Python算法之旅
4、Python魔法之旅
5、博客个人主页
  • 29
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

神奇夜光杯

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值