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

ops/2024/9/23 3:17:38/

目录

一、用法精讲

pandas.DataFrame.to_csv%E5%87%BD%E6%95%B0-toc" style="margin-left:120px;">231、pandas.Series.reorder_levels方法

231-1、语法

231-2、参数

231-3、功能

231-4、返回值

231-5、说明

231-6、用法

231-6-1、数据准备

231-6-2、代码示例

231-6-3、结果输出

pandas.Series.sort_values%E6%96%B9%E6%B3%95-toc" style="margin-left:120px;">232、pandas.Series.sort_values方法

232-1、语法

232-2、参数

232-3、功能

232-4、返回值

232-5、说明

232-6、用法

232-6-1、数据准备

232-6-2、代码示例

232-6-3、结果输出

pandas.Series.sort_index%E6%96%B9%E6%B3%95-toc" style="margin-left:120px;">233、pandas.Series.sort_index方法

233-1、语法

233-2、参数

233-3、功能

233-4、返回值

233-5、说明

233-6、用法

233-6-1、数据准备

233-6-2、代码示例

233-6-3、结果输出

pandas.Series.swaplevel%E6%96%B9%E6%B3%95-toc" style="margin-left:120px;">234、pandas.Series.swaplevel方法

234-1、语法

234-2、参数

234-3、功能

234-4、返回值

234-5、说明

234-6、用法

234-6-1、数据准备

234-6-2、代码示例

234-6-3、结果输出

pandas.Series.unstack%E6%96%B9%E6%B3%95-toc" style="margin-left:120px;">235、pandas.Series.unstack方法

235-1、语法

235-2、参数

235-3、功能

235-4、返回值

235-5、说明

235-6、用法

235-6-1、数据准备

235-6-2、代码示例

235-6-3、结果输出

二、推荐阅读

1、Python筑基之旅

2、Python函数之旅

3、Python算法之旅

4、Python魔法之旅

5、博客个人主页

一、用法精讲

pandas.DataFrame.to_csv%E5%87%BD%E6%95%B0">231、pandas.Series.reorder_levels方法
231-1、语法
python"># 231、pandas.Series.reorder_levels方法
pandas.Series.reorder_levels(order)
Rearrange index levels using input order.May not drop or duplicate levels.Parameters:
order
list of int representing new level order
Reference level by number or key.Returns:
type of caller (new object)
231-2、参数

231-2-1、order(必须)指定重新排序索引的顺序,它应该是一个列表或元组,包含索引级别的顺序,级别的索引可以通过名称或位置来指定。如果提供的顺序不包含所有的级别,reorder_levels方法会将缺少的级别保持不变。

231-3、功能

        允许你根据指定的顺序重新排序Series的多级索引,这对于调整数据的组织结构很有用,尤其是在进行数据透视或汇总时。

231-4、返回值

        返回一个新的Series对象,其中的索引按照指定的顺序重新排序。

231-5、说明

        无

231-6、用法
231-6-1、数据准备
python">无
231-6-2、代码示例
python"># 231、pandas.Series.reorder_levels方法
import pandas as pd
# 创建一个具有多级索引的Series
index = pd.MultiIndex.from_tuples([('A', 1), ('A', 2), ('B', 1), ('B', 2)], names=['letter', 'number'])
s = pd.Series([10, 20, 30, 40], index=index)
# 重新排序索引
s_reordered = s.reorder_levels(['number', 'letter'])
print(s_reordered)
231-6-3、结果输出
python"># 231、pandas.Series.reorder_levels方法
# number  letter
# 1       A         10
# 2       A         20
# 1       B         30
# 2       B         40
# dtype: int64
pandas.Series.sort_values%E6%96%B9%E6%B3%95">232、pandas.Series.sort_values方法
232-1、语法
python"># 232、pandas.Series.sort_values方法
pandas.Series.sort_values(*, axis=0, ascending=True, inplace=False, kind='quicksort', na_position='last', ignore_index=False, key=None)
Sort by the values.Sort a Series in ascending or descending order by some criterion.Parameters:
axis
{0 or ‘index’}
Unused. Parameter needed for compatibility with DataFrame.ascending
bool or list of bools, default True
If True, sort values in ascending order, otherwise descending.inplace
bool, default False
If True, perform operation in-place.kind
{‘quicksort’, ‘mergesort’, ‘heapsort’, ‘stable’}, default ‘quicksort’
Choice of sorting algorithm. See also numpy.sort() for more information. ‘mergesort’ and ‘stable’ are the only stable algorithms.na_position
{‘first’ or ‘last’}, default ‘last’
Argument ‘first’ puts NaNs at the beginning, ‘last’ puts NaNs at the end.ignore_index
bool, default False
If True, the resulting axis will be labeled 0, 1, …, n - 1.key
callable, optional
If not None, apply the key function to the series values before sorting. This is similar to the key argument in the builtin sorted() function, with the notable difference that this key function should be vectorized. It should expect a Series and return an array-like.Returns:
Series or None
Series ordered by values or None if inplace=True.
232-2、参数

232-2-1、axis(可选,默认值为0)指定排序的轴,对于Series对象,这个参数默认为0,即沿着数据的索引进行排序。

232-2-2、ascending(可选,默认值为True)指定排序的顺序,True表示升序(默认),False表示降序。如果提供的是一个布尔列表,它必须与Series的索引长度相同,且会对不同的排序级别进行不同的排序。

232-2-3、inplace(可选,默认值为False)是否在原地进行排序,True表示在原地排序并修改原始对象,False(默认)表示返回排序后的新对象。

232-2-4、kind(可选,默认值为'quicksort')排序算法类:'quicksort'、'mergesort'和'heapsort',任选其一。

232-2-5、na_position(可选,默认值为'last')NaN值的位置,可选值有'first'(默认)和'last',指定NaN值在排序后的结果中的位置。

232-2-6、ignore_index(可选,默认值为False)排序后是否重新生成索引,False(默认)表示保留原索引,True表示生成新的连续索引。

232-2-7、key(可选,默认值为None)函数,用于在排序前对数据进行转换,该函数会作用于每个数据值,然后基于转换后的值进行排序。

232-3、功能

        用于对Series对象的值进行排序,该方法支持多种排序选项,并可以根据需要进行配置。

232-4、返回值

        返回一个新的Series对象,其中的值按照指定的排序顺序排列,如果inplace=True,则对原对象进行修改,不返回新对象。

232-5、说明

        无

232-6、用法
232-6-1、数据准备
python">无
232-6-2、代码示例
python"># 232、pandas.Series.sort_values方法
import pandas as pd
# 创建一个Series对象
s = pd.Series([3, 6, 5, 11, 10, 8, 10, 24])
# 对Series进行升序排序
s_sorted = s.sort_values()
print(s_sorted, end='\n\n')
s_sorted = s.sort_values(ignore_index=True)
print(s_sorted)
232-6-3、结果输出
python"># 232、pandas.Series.sort_values方法
# 0     3
# 2     5
# 1     6
# 5     8
# 4    10
# 6    10
# 3    11
# 7    24
# dtype: int64
#
# 0     3
# 1     5
# 2     6
# 3     8
# 4    10
# 5    10
# 6    11
# 7    24
pandas.Series.sort_index%E6%96%B9%E6%B3%95">233、pandas.Series.sort_index方法
233-1、语法
python"># 233、pandas.Series.sort_index方法
pandas.Series.sort_index(*, axis=0, level=None, ascending=True, inplace=False, kind='quicksort', na_position='last', sort_remaining=True, ignore_index=False, key=None)
Sort Series by index labels.Returns a new Series sorted by label if inplace argument is False, otherwise updates the original series and returns None.Parameters:
axis
{0 or ‘index’}
Unused. Parameter needed for compatibility with DataFrame.level
int, optional
If not None, sort on values in specified index level(s).ascending
bool or list-like of bools, default True
Sort ascending vs. descending. When the index is a MultiIndex the sort direction can be controlled for each level individually.inplace
bool, default False
If True, perform operation in-place.kind
{‘quicksort’, ‘mergesort’, ‘heapsort’, ‘stable’}, default ‘quicksort’
Choice of sorting algorithm. See also numpy.sort() for more information. ‘mergesort’ and ‘stable’ are the only stable algorithms. For DataFrames, this option is only applied when sorting on a single column or label.na_position
{‘first’, ‘last’}, default ‘last’
If ‘first’ puts NaNs at the beginning, ‘last’ puts NaNs at the end. Not implemented for MultiIndex.sort_remaining
bool, default True
If True and sorting by level and index is multilevel, sort by other levels too (in order) after sorting by specified level.ignore_index
bool, default False
If True, the resulting axis will be labeled 0, 1, …, n - 1.key
callable, optional
If not None, apply the key function to the index values before sorting. This is similar to the key argument in the builtin sorted() function, with the notable difference that this key function should be vectorized. It should expect an Index and return an Index of the same shape.Returns:
Series or None
The original Series sorted by the labels or None if inplace=True.
233-2、参数

233-2-1、axis(可选,默认值为0)指定排序的轴,对于Series对象,通常设置为0,表示对索引进行排序。

233-2-2、level(可选,默认值为None)对多级索引(MultiIndex)进行排序时,指定排序的级别,可以是单个级别或级别的列表。

233-2-3、ascending(可选,默认值为True)指定排序的顺序,True表示升序(默认);False表示降序,如果提供的是一个布尔列表,它必须与索引的级别数相同。

233-2-4、inplace(可选,默认值为False)是否在原地进行排序,True表示在原地排序并修改原始对象,False(默认)表示返回排序后的新对象。

233-2-5、kind(可选,默认值为'quicksort')排序算法类:'quicksort'、'mergesort'和'heapsort',任选其一。

233-2-6、na_position(可选,默认值为'last')NaN值的位置,可选值有'first'(默认)和'last',指定NaN值在排序后的结果中的位置。

233-2-7、sort_remaining(可选,默认值为True)在MultiIndex中,是否对剩余的级别进行排序。True(默认)表示对所有级别进行排序;False表示仅对指定的级别排序。

233-2-8、ignore_index(可选,默认值为False)排序后是否重新生成索引,False(默认)表示保留原索引,True表示生成新的连续索引。

233-2-9、key(可选,默认值为None)函数,用于在排序前对索引进行转换,该函数会作用于每个索引值,然后基于转换后的值进行排序。

233-3、功能

        用于对Series对象的索引进行排序,它对索引进行排序而不是对数据值排序。

233-4、返回值

        返回一个新的Series对象,其中的索引按照指定的排序顺序排列,如果inplace=True,则对原对象进行修改,不返回新对象。

233-5、说明

        无

233-6、用法
233-6-1、数据准备
python">无
233-6-2、代码示例
python"># 233、pandas.Series.sort_index方法
import pandas as pd
# 创建一个Series对象
s = pd.Series([3, 1, 2, 5, 4], index=['b', 'a', 'c', 'e', 'd'])
# 对索引进行升序排序
s_sorted = s.sort_index()
print(s_sorted, end='\n\n')
s_sorted = s.sort_index(ignore_index=True)
print(s_sorted)
233-6-3、结果输出
python"># 233、pandas.Series.sort_index方法
# a    1
# b    3
# c    2
# d    4
# e    5
# dtype: int64
#
# 0    1
# 1    3
# 2    2
# 3    4
# 4    5
# dtype: int64
pandas.Series.swaplevel%E6%96%B9%E6%B3%95">234、pandas.Series.swaplevel方法
234-1、语法
python"># 234、pandas.Series.swaplevel方法
pandas.Series.swaplevel(i=-2, j=-1, copy=None)
Swap levels i and j in a MultiIndex.Default is to swap the two innermost levels of the index.Parameters:
i, jint or str
Levels of the indices to be swapped. Can pass level name as string.copybool, default True
Whether to copy underlying data.NoteThe copy keyword will change behavior in pandas 3.0. Copy-on-Write will be enabled by default, which means that all methods with a copy keyword will use a lazy copy mechanism to defer the copy and ignore the copy keyword. The copy keyword will be removed in a future version of pandas.You can already get the future behavior and improvements through enabling copy on write pd.options.mode.copy_on_write = TrueReturns:
Series
Series with levels swapped in MultiIndex.
234-2、参数

234-2-1、i(可选,默认值为-2)整数或字符串,表示要交换的第一个级别的标签或级别的索引位置。

234-2-2、j(可选,默认值为-1)整数或字符串,表示要交换的第二个级别的标签或级别的索引位置。

234-2-3、copy(可选,默认值为None)是否在交换级别时复制数据,True表示复制,False表示不复制,如果设为None,则取决于MultiIndex的具体情况。

234-3、功能

        用于交换Series对象中的多级索引(MultiIndex)的级别,该方法允许在MultiIndex中调换两个级别的位置。

234-4、返回值

        返回一个新的Series对象,其中指定的级别已被交换,如果copy=False,则可能对原对象进行修改(具体取决于索引的实现)。

234-5、说明

        无

234-6、用法
234-6-1、数据准备
python">无
234-6-2、代码示例
python"># 234、pandas.Series.swaplevel方法
import pandas as pd
# 创建一个MultiIndex的Series对象
index = pd.MultiIndex.from_tuples([('A', 1), ('A', 2), ('B', 1), ('B', 2)], names=['letter', 'number'])
s = pd.Series([10, 20, 30, 40], index=index)
# 打印原始Series
print("Original Series:")
print(s)
# 交换'letter'和'number'两个级别
s_swapped = s.swaplevel(i='letter', j='number')
print("\nSeries after swapping levels:")
print(s_swapped)
234-6-3、结果输出
python"># 234、pandas.Series.swaplevel方法
# Original Series:
# letter  number
# A       1         10
#         2         20
# B       1         30
#         2         40
# dtype: int64
#
# Series after swapping levels:
# number  letter
# 1       A         10
# 2       A         20
# 1       B         30
# 2       B         40
# dtype: int64
pandas.Series.unstack%E6%96%B9%E6%B3%95">235、pandas.Series.unstack方法
235-1、语法
python"># 235、pandas.Series.unstack方法
pandas.Series.unstack(level=-1, fill_value=None, sort=True)
Unstack, also known as pivot, Series with MultiIndex to produce DataFrame.Parameters:
level
int, str, or list of these, default last level
Level(s) to unstack, can pass level name.fill_value
scalar value, default None
Value to use when replacing NaN values.sort
bool, default True
Sort the level(s) in the resulting MultiIndex columns.Returns:
DataFrame
Unstacked Series.
235-2、参数

235-2-1、level(可选,默认值为-1)要转换为列的层级,默认值是-1,表示最后一个层级。如果Series是多层级的,level参数允许你选择要转换的层级,可以是单个整数、整数列表或层级名称(对于DataFrame)。

235-2-2、fill_value(可选,默认值为None)在转换过程中用于填充缺失值的值,默认是None,即不进行填充,如果你指定了一个值,那么在转换过程中,所有缺失的数据点将被填充为这个值。

235-2-3、sort(可选,默认值为True)是否对新生成的列进行排序,默认值是True,表示排序。如果设置为False,则保持原来的顺序,这对于保持数据的原始顺序很有用,尤其是在数据按照特定方式排序时。

235-3、功能

        将Series对象的指定层级的索引转换为DataFrame的列,这有助于将数据从长格式(即多级索引的Series)转换为宽格式(即普通的DataFrame),使数据更容易进行分析和操作。

235-4、返回值

        返回一个DataFrame,其中被转换为列的层级的索引现在成为DataFrame的列,原始的索引层级将被保留,或根据指定的层级被处理。

235-5、说明

        无

235-6、用法
235-6-1、数据准备
python">无
235-6-2、代码示例
python"># 235、pandas.Series.unstack方法
import pandas as pd
data = pd.Series([1, 2, 3, 4], index=[['New York', 'New York', 'Los Angeles', 'Los Angeles'], ['2023-01-01', '2023-01-02', '2023-01-01', '2023-01-02']])
print(data, end='\n\n')
df = data.unstack(level=-1)
print(df)
235-6-3、结果输出
python"># 235、pandas.Series.unstack方法
# New York     2023-01-01    1
#              2023-01-02    2
# Los Angeles  2023-01-01    3
#              2023-01-02    4
# dtype: int64
#
#              2023-01-01  2023-01-02
# Los Angeles           3           4
# New York              1           2

二、推荐阅读

1、Python筑基之旅
2、Python函数之旅
3、Python算法之旅
4、Python魔法之旅
5、博客个人主页

http://www.ppmy.cn/ops/87479.html

相关文章

精心准备的高水平的博客【点评语】,来抄啊!

大家好,我是一名_全栈_测试开发工程师,已经开源一套【自动化测试框架】和【测试管理平台】,欢迎大家关注我,和我一起【分享测试知识,交流测试技术,趣聊行业热点】。 第 1 条 这篇博客文章如同灯塔般照亮了技…

从入门到自动化:一篇文章掌握Python的80%

Python作为一种高级编程语言,以其简洁明了的语法和强大的功能性,在全球编程社区内享有极高的声誉。本文将带领你从Python的基础语法入手,介绍其常用库的应用,以及如何将Python用于数据分析、网络爬虫和简单的自动化任务&#xff0…

什么是负责任的人工智能

「AI秘籍」系列课程: 人工智能应用数学基础人工智能Python基础人工智能基础核心知识人工智能BI核心知识人工智能CV核心知识AI 进阶:企业项目实战 可直接在橱窗里购买,或者到文末领取优惠后购买: 拥有权利的同时也被赋予了重大的…

Python可视化开发全面教程

Python是一种功能强大且易于学习的编程语言,它还提供了丰富的可视化库,如Matplotlib、Seaborn、Plotly和Bokeh。这些库使得数据可视化变得简单而直观。在本教程中,我们将介绍如何使用Python进行数据可视化,从基础知识到高级技巧。…

crack 基于golang的多并发爆破工具

一款轻巧的内网弱口令枚举工具,项目地址:GitHub - oksbsb/crack: 支持 ftp ssh smb mysql mssql postgres 安装 ​ git clone https://github.com/yanweijin/crack go build main.go基于用法 ➜ crack git:(master) ✗ go run main.go --help …

盘点那些实用的开发技术!!

敏感信息加密操作,让开发的系统更加的安全可靠!!Jasypt(Java Simplified Encryption)是一个开源的Java库,用于简化加密操作。https://mp.weixin.qq.com/s/sPBV8Ej46YJsElImodRjAQ每个Web开发都应了解的&…

一款.NET开源、跨平台的DASH/HLS/MSS下载工具

前言 今天大姚给大家分享一款.NET开源(MIT License)、免费、跨平台的DASH/HLS/MSS下载工具,并且支持点播和直播(DASH/HLS)的内容下载:N_m3u8DL-RE。 网络流媒体传输协议介绍 DASH DASH是一种基于HTTP的…

数据同步策略概览

数据同步在业务开发中比较普遍,例如 订阅MySQL的binlog将数据同步至异构数据库。数据同步方案需要考虑一下几点: 数据实时性要求数据量级是否有数据转换逻辑 可分为两种模式 发布订阅模式:分为订阅数据库log还是订阅应用层发的消息点对点模…