iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >Python数据处理之pd.Series()函数的基本使用
  • 274
分享到

Python数据处理之pd.Series()函数的基本使用

2024-04-02 19:04:59 274人浏览 安东尼

Python 官方文档:入门教程 => 点击学习

摘要

目录1.Series介绍2.Series创建1.pd.Series([list],index=[list])2.pd.Series(np.arange())3 Series基本属性4

1.Series介绍

pandas模块的数据结构主要有两种:1.Series 2.DataFrame

Series 是一维数组,基于Numpy的ndarray 结构

Series([data, index, dtype, name, copy, …])    
# One-dimensional ndarray with axis labels (including time series).

2.Series创建

import Pandas as pd 
import numpy as np

1.pd.Series([list],index=[list])

参数为list ,index为可选参数,若不填写则默认为index从0开始

obj = pd.Series([4, 7, -5, 3, 7, np.nan])
obj

输出结果为:

0    4.0
1    7.0
2   -5.0
3    3.0
4    7.0
5    NaN
dtype: float64

2.pd.Series(np.arange())

arr = np.arange(6)
s = pd.Series(arr)
s

输出结果为:

0    0
1    1
2    2
3    3
4    4
5    5
dtype: int32

pd.Series({dict})
d = {'a':10,'b':20,'c':30,'d':40,'e':50}
s = pd.Series(d)
s

输出结果为:

a    10
b    20
c    30
d    40
e    50
dtype: int64

可以通过DataFrame中某一行或者某一列创建序列

3 Series基本属性

  • Series.values:Return Series as ndarray or ndarray-like depending on the dtype
obj.values
# array([ 4.,  7., -5.,  3.,  7., nan])
  • Series.index:The index (axis labels) of the Series.
obj.index
# RangeIndex(start=0, stop=6, step=1)
  • Series.name:Return name of the Series.

4 索引

  • Series.loc:Access a group of rows and columns by label(s) or a boolean array.
  • Series.iloc:Purely integer-location based indexing for selection by position.

5 计算、描述性统计

 Series.value_counts:Return a Series containing counts of unique values.

index = ['Bob', 'Steve', 'Jeff', 'Ryan', 'Jeff', 'Ryan'] 
obj = pd.Series([4, 7, -5, 3, 7, np.nan],index = index)
obj.value_counts()

输出结果为:

 7.0    2
 3.0    1
-5.0    1
 4.0    1
dtype: int64

6 排序

Series.sort_values

Series.sort_values(self, axis=0, ascending=True, inplace=False, kind='quicksort', na_position='last')

Parameters:

ParametersDescription
axis{0 or ‘index’}, default 0,Axis to direct sorting. The value ‘index’ is accepted for compatibility with DataFrame.sort_values.
ascendinbool, default True,If True, sort values in ascending order, otherwise descending.
inplacebool, default FalseIf True, perfORM operation in-place.
kind{‘quicksort’, ‘mergesort’ or ‘heapsort’}, default ‘quicksort’Choice of sorting alGorithm. See also numpy.sort() for more information. ‘mergesort’ is the only stable algorithm.
na_position{‘first’ or ‘last’}, default ‘last’,Argument ‘first’ puts NaNs at the beginning, ‘last’ puts NaNs at the end.

Returns:

Series:Series ordered by values.

obj.sort_values()

输出结果为:

Jeff    -5.0
Ryan     3.0
Bob      4.0
Steve    7.0
Jeff     7.0
Ryan     NaN
dtype: float64

  • Series.rank
Series.rank(self, axis=0, method='average', numeric_only=None, na_option='keep', ascending=True, pct=False)[source]

Parameters:

ParametersDescription
axis{0 or ‘index’, 1 or ‘columns’}, default 0Index to direct ranking.
method{‘average’, ‘min’, ‘max’, ‘first’, ‘dense’}, default ‘average’How to rank the group of records that have the same value (i.e. ties): average, average rank of the group; min: lowest rank in the group; max: highest rank in the group; first: ranks assigned in order they appear in the array; dense: like ‘min’, but rank always increases by 1,between groups
numeric_onlybool, optional,For DataFrame objects, rank only numeric columns if set to True.
na_option{‘keep’, ‘top’, ‘bottom’}, default ‘keep’, How to rank NaN values:;keep: assign NaN rank to NaN values; top: assign smallest rank to NaN values if ascending; bottom: assign highest rank to NaN values if ascending
ascendingbool, default True Whether or not the elements should be ranked in ascending order.
pctbool, default False Whether or not to display the returned rankings in percentile form.

Returns:

same type as caller :Return a Series or DataFrame with data ranks as values.

# obj.rank()            #从大到小排,NaN还是NaN
obj.rank(method='dense')  
# obj.rank(method='min')
# obj.rank(method='max')
# obj.rank(method='first')
# obj.rank(method='dense')

输出结果为:

Bob      3.0
Steve    4.0
Jeff     1.0
Ryan     2.0
Jeff     4.0
Ryan     NaN
dtype: float64

总结

到此这篇关于python数据处理之pd.Series()函数的基本使用的文章就介绍到这了,更多相关Python pd.Series()函数内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: Python数据处理之pd.Series()函数的基本使用

本文链接: https://www.lsjlt.com/news/118826.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Python数据处理之pd.Series()函数的基本使用
    目录1.Series介绍2.Series创建1.pd.Series([list],index=[list])2.pd.Series(np.arange())3 Series基本属性4...
    99+
    2024-04-02
  • Python数据处理之pd.Series()函数怎么使用
    本文小编为大家详细介绍“Python数据处理之pd.Series()函数怎么使用”,内容详细,步骤清晰,细节处理妥当,希望这篇“Python数据处理之pd.Series()函数怎么使用”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一...
    99+
    2023-07-02
  • python中super()函数的理解与基本使用
    目录前言super的用法 super的原理 Python super()使用注意事项混用super与显式类调用 不同种类的参数 总结 前言 Python是一门面向对象的语言,定义类时...
    99+
    2024-04-02
  • Python的基本数据类型之Number
    Python下载地址: https://www.python.org/downloads/ 部分参考资料:廖雪峰的网站 Python与Java在一定程度上比较相似,都是面向对象型的语言。首先搭配好Python的开发环境,网上相关...
    99+
    2023-01-31
    数据类型 Python Number
  • python基础篇之pandas常用基本函数汇总
    目录前言1、汇总函数2、特征统计函数3、唯一值函数4、替换函数总结前言 这篇主要整理pandas常用的基本函数,主要分为五部分: 汇总函数特征统计函数唯一值函数替换函数排序函数 1、...
    99+
    2024-04-02
  • 怎么使用Python处理文本数据
    本篇内容介绍了“怎么使用Python处理文本数据”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!用python处理文本数据实验目的熟悉pyth...
    99+
    2023-07-02
  • python数据分析基础知识之shape()函数的使用教程
    目录python中shape()函数1、shape()输入参数2、判断数组的维度总结python中shape()函数 shape函数是numpy.core.fromnumeric中的...
    99+
    2024-04-02
  • Python OpenCV的基本使用及相关函数
    目录1、图像的读取2、图像保存3、图像展示4、获取图像属性5、图像缩放(宽,高)6、在原图像中获取某一区域7、彩色图像通道分解8、图像加法9、图像反转10、图像金字塔11、直方图12...
    99+
    2024-04-02
  • python基础之 函数的参数
      一、形参和实参是什么  函数的参数分为形式参数和实际参数,简称形参和实参:  形参即在定义函数时,括号内声明的参数。形参本质就是一个变量名,用来接收外部传来的值。  def func(x,y): #x和y为形参  print(x,y) ...
    99+
    2023-06-01
  • Java基础之详解基本数据类型的使用
    一、整型 主要扩展一下不同进制的整型 二进制、八进制、十进制、十六进制 * 二进制 : 0B(数字零+B) 0b(数字零+b) * 八进制 :0(数字零开头) * 十进制 :正常写...
    99+
    2024-04-02
  • python数字图像处理之基本图形的绘制
    目录引言1、画线条2、画圆3、多边形4、椭圆5、贝塞儿曲线6、画空心圆7、空心椭圆引言 图形包括线条、圆形、椭圆形、多边形等。 在skimage包中,绘制图形用的是draw模块,不要...
    99+
    2024-04-02
  • Python全栈之基本数据类型
    目录1. number类型1.1 int整型1.2 float浮点型(小数)1.3 bool布尔型1.4 复数类型2. 字符串类型3. 列表_元组_字符串3.1 列表类型3.2 元组...
    99+
    2024-04-02
  • python基本数据类型之字典
    字典的定义与特性字典是Python语言中唯一的映射类型。定义:{key1:value1,key2:value2}1、键与值用冒号“:”分开; 2、项与项用逗号“,”分开;特性:1.key-value结构 2.key必须可hash、且必须为不...
    99+
    2023-01-31
    字典 数据类型 python
  • 使用Python解析JSON数据的基本方
    Python的json模块提供了一种很简单的方式来编码和解码JSON数据。 其中两个主要的函数是 json.dumps() 和 json.loads() , 要比其他序列化函数库如pickle的接口少得多。 下面演示如何将一个...
    99+
    2023-01-31
    数据 Python JSON
  • Python中range函数的基本用法
    Python中range()是一个内置函数,用于生成一个整数序列,其基本语法为“range(start, stop[, step])”,其中,start表示序列的起始值(可以省略,默认为0),stop表示序列的结束值(必须指定),...
    99+
    2024-01-26
    python range函数
  • Python 文件处理之open()函数
    目录1.文件处理2.Python 文件写入3.创建新文件4.删除文件实例删除文件夹前言: 文件处理是任何 Web 应用程序的重要组成部分。 Python 有几个用于创建、读取、更新和...
    99+
    2024-04-02
  • 如何使用Python中的字符串操作函数处理大规模文本数据
    如何使用Python中的字符串操作函数处理大规模文本数据,需要具体代码示例随着互联网的快速发展和数据的不断增加,大规模文本数据处理成了现代科技中的一个重要课题。Python作为一门简单易学且功能强大的编程语言,提供了丰富的字符串操作函数,能...
    99+
    2023-10-22
    字符串操作 Python编程 大规模数据
  • vue3 中的toRef函数和toRefs函数的基本使用
    目录这篇我们看下toRef和toRefs的基本使用Vue3的toRef这篇我们看下toRef和toRefs的基本使用 我们知道ref可以用于创建一个响应式数据,而toRef也可以创建...
    99+
    2022-11-16
    vue3 toRef和toRefs函数 vue3 toRef函数和toRefs函数 vue3 toRef函数
  • Python基础之sorted()函数用法
    本篇是关于sorted()函数的一些基本用法,如有不足缺陷欢迎补充指正。 1、简单的排序 sorted函数可以对可迭代类型的容器内的数据进行排序 lst1 = (5,4,3,2,1)lst2 = ('...
    99+
    2023-10-12
    python
  • PHP函数的基本使用方法
    在现代的网页应用程序中,PHP作为一种重要的服务器端编程语言,被广泛应用。PHP中的函数是一种可重用的代码块,不仅简化了代码的编写,而且提高了代码的可维护性和可读性。本文将介绍PHP函数的基本使用方法,以帮助读者更好地理解和应用PHP函数。...
    99+
    2023-05-22
    方法 PHP函数 基本使用
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作