Python数据处理之pd.Series()函数的基本使用
作者:流年里不舍的执着 发布时间:2022-09-29 08:50:21
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:
Parameters | Description |
---|---|
axis | {0 or ‘index’}, default 0,Axis to direct sorting. The value ‘index’ is accepted for compatibility with DataFrame.sort_values. |
ascendin | bool, default True,If True, sort values in ascending order, otherwise descending. |
inplace | bool, 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:
Parameters | Description |
---|---|
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_only | bool, 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 |
ascending | bool, default True Whether or not the elements should be ranked in ascending order. |
pct | bool, 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
来源:https://blog.csdn.net/weixin_43868107/article/details/102631717
猜你喜欢
- vue配置element-ui遇到的坑注意:本文章参照element-ui官方文档,快速上手部分,的部分教程步骤1.npm安装npm i e
- 俺比较笨,对太专业的书一直不感冒,看了就想睡觉。最近李明同学传了本“大话设计模式”电子版。偶然翻了翻,感觉还满通俗的,正适合我这样的懒人学习
- 回想下,在 Python 中编程时,你是否曾经需要检查某个可迭代对象(如列表)中的任何元素或所有元素的计算结果是否为True?假设,我们要判
- Python Flask项目中获取请求用户IP地址 addr服务器直接部署Flaskimport loggingfrom flask imp
- 1.简介celery(芹菜)是一个异步任务队列/基于分布式消息传递的作业队列。它侧重于实时操作,但对调度支持也很好。celery用于生产系统
- 前几天一直在寻找能够输出python函数运行时最大内存消耗的方式,看了一堆的博客和知乎,也尝试了很多方法,最后选择使用memory_prof
- 目录友情提示:性能优化指标数据库查询优化利用Queryset的惰性和缓存,避免重复查询一次查询所有需要的关联模型数据仅查询需要用到的数据使用
- 对于使用已经训练好的模型,比如VGG,RESNET等,keras都自带了一个keras.applications.imagenet_util
- 最近终于找到一个好的方法,使用Python的OpenCV模块识别滑动验证码的缺口,可以将滑动验证码中的缺口识别出来了。 测试使用如
- 关联2张表时出现了无法创建外键的情况,从这个博客看到,问题出在第六点的Charset和Collate选项在表级和字段级上的一致性上。我的2张
- Python最近挺火呀,比鹿晗薛之谦还要火,当然是在程序员之间。下面我们看看有关Python的相关内容。上一篇文章我们已经介绍了部分Pyth
- rs.open sql,conn,A,B A: ADOPenforwardonly (=0) 只读,且当前数据记录只能向下移动。 ADOPe
- CentOS6.9+Mysql5.7.18源码安装,以下操作均在root用户下执行。1、安装依赖工具cmake make3.75+ gcc4
- sys.argv[]是用来获取命令行参数的,sys.argv[0]表示代码本身文件路径,所以参数从1开始,以下两个例子说明:1、使用sys.
- 一、 背景介绍web应用采用的是ssh框架,数据库使用的sql server2014版本。二、问题:客户要求,ID列的数据类型必须是uniq
- 0x01 生成shellcode首先通过下列命令生成一个shellcode,使用msfvenom -p选项来指定paylaod,这里选用wi
- 很久没有更新blog了,这段时间实在是发生了很多的事,累身累心。但还是有很多想做的事,比如更新merceCSS、把一直以来所总结的有关模块化
- 安装模块下面需要用模块,先安装一下:pip install numpy pip install opencv-python==4.5.5.6
- python是支持多线程的,并且是native的线程。主要是通过thread和threading这两个模块来实现的。thread是比较底层的
- 几个星期前,SQL Server 2016的最新CTP版本已经发布了:CTP 2.4(目前已经是CTP 3.0)。这个预览版相比以前的CTP