dubbo官方文档中文_Pandas中文官方文档:基础用法5
本文授权转载自Python大咖谈禁止二次转载
大家好,我是老表
阅读文本大概需要 16 分钟
Pandas中文官方文档:基础用法1
Pandas中文官方文档:基础用法2
Pandas中文官方文档:基础用法3
Pandas中文官方文档:基础用法4
.dt 访问器
Series
提供了一个可以简单、快捷返回 datetime
属性值的访问器。这个访问器返回的也是 Series,索引与现有的 Series 一样。
# datetimeIn [264]: s = pd.Series(pd.date_range('20130101 09:10:12', periods=4))
In [265]: sOut[265]: 0 2013-01-01 09:10:121 2013-01-02 09:10:122 2013-01-03 09:10:123 2013-01-04 09:10:12dtype: datetime64[ns]
In [266]: s.dt.hourOut[266]: 0 91 92 93 9dtype: int64
In [267]: s.dt.secondOut[267]: 0 121 122 123 12dtype: int64
In [268]: s.dt.dayOut[268]: 0 11 22 33 4dtype: int64
用下列表达式进行筛选非常方便:
In [269]: s[s.dt.day == 2]Out[269]: 1 2013-01-02 09:10:12dtype: datetime64[ns]
还可以轻易实现时区转换:
In [270]: stz = s.dt.tz_localize('US/Eastern')
In [271]: stzOut[271]: 0 2013-01-01 09:10:12-05:001 2013-01-02 09:10:12-05:002 2013-01-03 09:10:12-05:003 2013-01-04 09:10:12-05:00dtype: datetime64[ns, US/Eastern]
In [272]: stz.dt.tzOut[272]: 'US/Eastern' LMT-1 day, 19:04:00 STD>
还可以把这些操作连在一起:
In [273]: s.dt.tz_localize('UTC').dt.tz_convert('US/Eastern')Out[273]: 0 2013-01-01 04:10:12-05:001 2013-01-02 04:10:12-05:002 2013-01-03 04:10:12-05:003 2013-01-04 04:10:12-05:00dtype: datetime64[ns, US/Eastern]
还可以用 Series.dt.strftime()
把 datetime
的值当成字符串进行格式化,支持与标准的 strftime()
同样的格式。
# DatetimeIndexIn [274]: s = pd.Series(pd.date_range('20130101', periods=4))
In [275]: sOut[275]: 0 2013-01-011 2013-01-022 2013-01-033 2013-01-04dtype: datetime64[ns]
In [276]: s.dt.strftime('%Y/%m/%d')Out[276]: 0 2013/01/011 2013/01/022 2013/01/033 2013/01/04dtype: object
# PeriodIndexIn [277]: s = pd.Series(pd.period_range('20130101', periods=4))
In [278]: sOut[278]: 0 2013-01-011 2013-01-022 2013-01-033 2013-01-04dtype: period[D]
In [279]: s.dt.strftime('%Y/%m/%d')Out[279]: 0 2013/01/011 2013/01/022 2013/01/033 2013/01/04dtype: object
.dt
访问器还支持 period
与 timedelta
。
# periodIn [280]: s = pd.Series(pd.period_range('20130101', periods=4, freq='D'))
In [281]: sOut[281]: 0 2013-01-011 2013-01-022 2013-01-033 2013-01-04dtype: period[D]
In [282]: s.dt.yearOut[282]: 0 20131 20132 20133 2013dtype: int64
In [283]: s.dt.dayOut[283]: 0 11 22 33 4dtype: int64
# timedeltaIn [284]: s = pd.Series(pd.timedelta_range('1 day 00:00:05', periods=4, freq='s'))
In [285]: sOut[285]: 0 1 days 00:00:051 1 days 00:00:062 1 days 00:00:073 1 days 00:00:08dtype: timedelta64[ns]
In [286]: s.dt.daysOut[286]: 0 11 12 13 1dtype: int64
In [287]: s.dt.secondsOut[287]: 0 51 62 73 8dtype: int64
In [288]: s.dt.componentsOut[288]: days hours minutes seconds milliseconds microseconds nanoseconds0 1 0 0 5 0 0 01 1 0 0 6 0 0 02 1 0 0 7 0 0 03 1 0 0 8 0 0 0
::: tip 注意
用这个访问器处理不是 datetime
类型的值时,Series.dt
会触发 TypeError
错误。
:::
矢量化字符串方法
Series 支持字符串处理方法,操作数组中每个元素十分方便。这些方法会自动排除缺失值与空值,这也许是其最重要的特性。这些方法通过 Series 的 str
属性访问,一般情况下,这些操作的名称与内置的字符串方法一致。示例如下:
In [289]: s = pd.Series(['A', 'B', 'C', 'Aaba', 'Baca', np.nan, 'CABA', 'dog', 'cat'])
In [290]: s.str.lower()Out[290]: 0 a1 b2 c3 aaba4 baca5 NaN6 caba7 dog8 catdtype: object
这里还提供了强大的模式匹配方法,但工业注意,模式匹配方法默认使用正则表达式。
参阅矢量化字符串方法了解完整内容。
排序
Pandas 支持三种排序方式,按索引标签排序,按列里的值排序,按两种方式混合排序。
按索引排序
Series.sort_index()
与 DataFrame.sort_index()
方法用于按索引层级对 pandas 对象排序。
In [291]: df = pd.DataFrame({ .....: 'one': pd.Series(np.random.randn(3), index=['a', 'b', 'c']), .....: 'two': pd.Series(np.random.randn(4), index=['a', 'b', 'c', 'd']), .....: 'three': pd.Series(np.random.randn(3), index=['b', 'c', 'd'])}) .....:
In [292]: unsorted_df = df.reindex(index=['a', 'd', 'c', 'b'], .....: columns=['three', 'two', 'one']) .....:
In [293]: unsorted_dfOut[293]: three two onea NaN -1.152244 0.562973d -0.252916 -0.109597 NaNc 1.273388 -0.167123 0.640382b -0.098217 0.009797 -1.299504
# DataFrameIn [294]: unsorted_df.sort_index()Out[294]: three two onea NaN -1.152244 0.562973b -0.098217 0.009797 -1.299504c 1.273388 -0.167123 0.640382d -0.252916 -0.109597 NaN
In [295]: unsorted_df.sort_index(ascending=False)Out[295]: three two oned -0.252916 -0.109597 NaNc 1.273388 -0.167123 0.640382b -0.098217 0.009797 -1.299504a NaN -1.152244 0.562973
In [296]: unsorted_df.sort_index(axis=1)Out[296]: one three twoa 0.562973 NaN -1.152244d NaN -0.252916 -0.109597c 0.640382 1.273388 -0.167123b -1.299504 -0.098217 0.009797
# SeriesIn [297]: unsorted_df['three'].sort_index()Out[297]: a NaNb -0.098217c 1.273388d -0.252916Name: three, dtype: float64
按值排序
Series.sort_values()
方法用于按值对 Series 排序。DataFrame.sort_values()
方法用于按行列的值对 DataFrame 排序。DataFrame.sort_values()
的可选参数 by
用于指定按哪列排序,该参数的值可以是一列或多列数据。
In [298]: df1 = pd.DataFrame({'one': [2, 1, 1, 1], .....: 'two': [1, 3, 2, 4], .....: 'three': [5, 4, 3, 2]}) .....:
In [299]: df1.sort_values(by='two')Out[299]: one two three0 2 1 52 1 2 31 1 3 43 1 4 2
参数 by
支持列名列表,示例如下:
In [300]: df1[['one', 'two', 'three']].sort_values(by=['one', 'two'])Out[300]: one two three2 1 2 31 1 3 43 1 4 20 2 1 5
这些方法支持用 na_position
参数处理空值。
In [301]: s[2] = np.nan
In [302]: s.sort_values()Out[302]: 0 A3 Aaba1 B4 Baca6 CABA8 cat7 dog2 NaN5 NaNdtype: object
In [303]: s.sort_values(na_position='first')Out[303]: 2 NaN5 NaN0 A3 Aaba1 B4 Baca6 CABA8 cat7 dogdtype: object
按索引与值排序
0.23.0 版新增。
通过参数 by
传递给 DataFrame.sort_values()
的字符串可以引用列或索引层名。
# 创建 MultiIndexIn [304]: idx = pd.MultiIndex.from_tuples([('a', 1), ('a', 2), ('a', 2), .....: ('b', 2), ('b', 1), ('b', 1)]) .....:
In [305]: idx.names = ['first', 'second']
# 创建 DataFrameIn [306]: df_multi = pd.DataFrame({'A': np.arange(6, 0, -1)}, .....: index=idx) .....:
In [307]: df_multiOut[307]: Afirst second a 1 6 2 5 2 4b 2 3 1 2 1 1
按 second
(索引)与 A
(列)排序。
In [308]: df_multi.sort_values(by=['second', 'A'])Out[308]: Afirst second b 1 1 1 2a 1 6b 2 3a 2 4 2 5
::: tip 注意
如果字符串、列名、索引层名重名,会触发警告提示,并以列名为准。后期版本中,这种情况将会触发模糊错误。
:::
搜索排序
Series 支持 searchsorted()
方法,这与numpy.ndarray.searchsorted()
的操作方式类似。
In [309]: ser = pd.Series([1, 2, 3])
In [310]: ser.searchsorted([0, 3])Out[310]: array([0, 2])
In [311]: ser.searchsorted([0, 4])Out[311]: array([0, 3])
In [312]: ser.searchsorted([1, 3], side='right')Out[312]: array([1, 3])
In [313]: ser.searchsorted([1, 3], side='left')Out[313]: array([0, 2])
In [314]: ser = pd.Series([3, 1, 2])
In [315]: ser.searchsorted([0, 3], sorter=np.argsort(ser))Out[315]: array([0, 2])
最大值与最小值
Series 支持 nsmallest()
与 nlargest()
方法,本方法返回 N 个最大或最小的值。对于数据量大的 Series
来说,该方法比先为整个 Series 排序,再调用 head(n)
这种方式的速度要快得多。
In [316]: s = pd.Series(np.random.permutation(10))
In [317]: sOut[317]: 0 21 02 33 74 15 56 97 68 89 4dtype: int64
In [318]: s.sort_values()Out[318]: 1 04 10 22 39 45 57 63 78 86 9dtype: int64
In [319]: s.nsmallest(3)Out[319]: 1 04 10 2dtype: int64
In [320]: s.nlargest(3)Out[320]: 6 98 83 7dtype: int64
DataFrame
也支持 nlargest
与 nsmallest
方法。
In [321]: df = pd.DataFrame({'a': [-2, -1, 1, 10, 8, 11, -1], .....: 'b': list('abdceff'), .....: 'c': [1.0, 2.0, 4.0, 3.2, np.nan, 3.0, 4.0]}) .....:
In [322]: df.nlargest(3, 'a')Out[322]: a b c5 11 f 3.03 10 c 3.24 8 e NaN
In [323]: df.nlargest(5, ['a', 'c'])Out[323]: a b c5 11 f 3.03 10 c 3.24 8 e NaN2 1 d 4.06 -1 f 4.0
In [324]: df.nsmallest(3, 'a')Out[324]: a b c0 -2 a 1.01 -1 b 2.06 -1 f 4.0
In [325]: df.nsmallest(5, ['a', 'c'])Out[325]: a b c0 -2 a 1.01 -1 b 2.06 -1 f 4.02 1 d 4.04 8 e NaN
用多重索引的列排序
列为多重索引时,还可以显式排序,用 by
可以指定所有层级。
In [326]: df1.columns = pd.MultiIndex.from_tuples([('a', 'one'), .....: ('a', 'two'), .....: ('b', 'three')]) .....:
In [327]: df1.sort_values(by=('a', 'two'))Out[327]: a b one two three0 2 1 52 1 2 31 1 3 43 1 4 2
复制
在 pandas 对象上执行 copy()
方法,将复制底层数据(但不包括轴索引,因为轴索引不可变),并返回一个新的对象。注意,复制对象这种操作一般来说不是必须的。比如说,以下几种方式可以就地(inplace) 改变 DataFrame:
插入、删除、修改列
为
index
或columns
属性赋值对于同质数据,用
values
属性或高级索引即可直接修改值
注意,用 pandas 方法修改数据不会带来任何副作用,几乎所有方法都返回新的对象,不会修改原始数据对象。如果原始数据有所改动,唯一的可能就是用户显式指定了要修改原始数据。
大家好,我是老表
觉得本文不错的话,转发、留言、点赞,是对我最大的支持。
每日留言说说你读完本文感受?或者一句激励自己的话?(字数不少于15字)留言赠书
《深入浅出Python机器学习》
点击视频查看书籍介绍
长按扫码查看书籍详情
怎么加入刻意学习队伍点我,看文末彩蛋留言有啥福利点我就知道了想进学习交流群加微信:jjxksa888备注:简说Python2小时快速掌握Python基础知识要点。
完整Python基础知识要点
Python小知识 | 这些技能你不会?(一)Python小知识 | 这些技能你不会?(二)Python小知识 | 这些技能你不会?(三)Python小知识 | 这些技能你不会?(四)
dubbo官方文档中文_Pandas中文官方文档:基础用法5相关推荐
- activiti 7中文文档_Python3.7中文官方文档来袭...
对于很多新手而言,面对下面这样的文档会无从下手,表示被拒在大门外,而陷入迷茫. But,作为新手的你,有福了,官方中文版来袭了... 是这样的.... 来来来 ,先看下Python3.7有哪些新变化 ...
- python官方文档中文下载-python中文官方文档 PDF 下载
Python 2 教程 Python是一门简单易学,功能强大的编程语言.它具有高效的高级数据结构和简单而有效的 面向对象编程方法.Python优雅的语法和动态类型以及其解释性的性质,使它在许多领域和 ...
- Dubbo中文官方文档
Dubbo中文官方文档 由于市面并没有dubbo相关的书籍,最好的自学方式除了看教学视频就只能看官方文档了,小伙伴找不到中文官方文档的,可以用我的url,一起学习! http://dubbo.apac ...
- k8s1.18多master节点高可用集群安装-超详细中文官方文档
kubernetes安装系列文章 kubernetes1.17.3安装-超详细的安装步骤 安装kubernetes1.17.3多master节点的高可用集群 k8s1.18单master节点高可用集群 ...
- springboot中文官方文档
springboot中文官方文档 https://www.breakyizhan.com/springboot/3028.html spring框架 https://www.breakyizhan.c ...
- opencv4.1中文官方文档
我用阿里云盘分享了「opencv 4.1中文官方文档v1.1版.pdf」,你可以不限速下载
- Spring Framework 中文官方文档
Spring Framework 中文官方文档 目前为最新版 5.3.11. 其他spring文档不断更新中 1.历史.设计理念.反馈.入门. Overview 2.IoC 容器.事件.资源.i18n ...
- 极品飞车20ps4服务器位置,极品飞车20官网在哪?中文官网地址一览
极品飞车20官网在哪?中文官网地址一览 2017-06-04 11:36:25来源:游戏下载编辑:评论(0) <极品飞车20:复仇>已经正式公布,官方近日正式宣布中文官网上线.中文官网在哪 ...
- Spring中文官方文档(中文)
以下所有文档均包含多个版本,并支持多语言(英文及中文). Spring Boot 中文文档 https://www.docs4dev.com/docs/zh/spring-boot/1.5.9.REL ...
最新文章
- VsCode 开发工具中英文切换
- 计算机入门新人必学,异世修真人怎么玩?新手快速入门必备技巧
- viewflipper动画切换屏幕
- win7如何更改文件类型_教你win7局域网设置方法
- java+testng接口测试入门
- 多线程编程学习笔记——任务并行库(三)
- 微信小程序wss报错:wx.request:fail ssl hand shake error 解决方法
- AndroidStudio_安卓原生开发_java.io.FileNotFoundException: http://172.19.128.5(拒绝访问)---Android原生开发工作笔记157
- 数据库服务器操作系统升级方案,PostgreSQL 数据库跨版本升级常用方案解析
- GitHub的安装及使用
- 飞思卡尔16位单片机(二)——CodeWarrior开发环境搭建及新建、调试工程
- Mac上的windows 10系统,bootcamp 5更新 bootcamp 6,缺少bootcamp.msi
- hsf 架构_HSF源码剖析
- 费用型采购订单后台配置
- 单一窗口关区备案_【亚东关务】单一窗口集中申报来了,集报备案、集报清单详细操作手册...
- 甲减、甲状腺相关疾病最新研究、治疗进展(2021年下半年)
- Java Security Architecture--Java安全体系技术文档翻译(三)
- 北上深杭互联网薪资岗位统计
- 长城汽车室外AGV无线应用方案
- 迪赛智慧数——柱状图(象形标识图):在选择另一半时,你更看重的是?