Pandas | 05 基本功能
到目前为止,我们了解了三种Pandas数据结构以及如何创建它们。接下来将主要关注数据帧(DataFrame)对象,因为它在实时数据处理中非常重要,并且还讨论其他数据结构。
一、系列基本功能
编号 | 属性或方法 | 描述 |
---|---|---|
1 | axes |
返回行轴标签列表。 |
2 | dtype |
返回对象的数据类型(dtype )。 |
3 | empty |
如果系列为空,则返回True 。 |
4 | ndim |
返回底层数据的维数,默认定义:1 。 |
5 | size |
返回基础数据中的元素数。 |
6 | values |
将系列作为ndarray 返回。 |
7 | head() |
返回前n 行。 |
8 | tail() |
返回最后n 行。 |
现在创建一个系列并演示如何使用上面所有列出的属性操作。
import pandas as pd
import numpy as np s = pd.Series(np.random.randn(4))
print(s)
输出结果:
0 0.967853
1 -0.148368
2 -1.395906
3 -1.758394
dtype: float64
axes示例
import pandas as pd
import numpy as np s = pd.Series(np.random.randn(4))
print ("The axes are:")
print(s.axes)
输出结果:
The axes are:
[RangeIndex(start=0, stop=4, step=1)]
empty示例
返回布尔值,表示对象是否为空。返回True
则表示对象为空。
import pandas as pd
import numpy as np s = pd.Series(np.random.randn(4))
print ("Is the Object empty?")
print(s.empty)
输出结果:
Is the Object empty?
False
ndim示例
返回对象的维数。根据定义,一个系列是一个1D
数据结构,参考以下示例代码
import pandas as pd
import numpy as np s = pd.Series(np.random.randn(4))
print(s)
print('\n') print ("The dimensions of the object:",s.ndim)
输出结果 -
0 0.175898
1 0.166197
2 -0.609712
3 -1.377000
dtype: float64
The dimensions of the object:1
size示例
返回系列的大小(长度)。参考以下示例代码
import pandas as pd
import numpy as np s = pd.Series(np.random.randn(2))
print(s)
print('\n') print ("The size of the object:",s.size)
输出结果:
0 3.078058
1 -1.207803
dtype: float64
The size of the object:2
values示例
以数组形式返回系列中的实际数据值。
import pandas as pd
import numpy as np s = pd.Series(np.random.randn(4))
print(s)
print('\n') print ("The actual data series is:",s.values)
输出结果:
0 1.787373
1 -0.605159
2 0.180477
3 -0.140922
dtype: float64
The actual data series is:[ 1.78737302 -0.60515881 0.18047664 -0.1409218 ]
head()和tail()方法示例
要查看Series或DataFrame对象的小样本,请使用head()
和tail()
方法。
head()
返回前n
行(观察索引值)。要显示的元素的默认数量为5
,但可以传递自定义这个数字值。
tail()
返回最后n
行(观察索引值)。 要显示的元素的默认数量为5
,但可以传递自定义数字值。
import pandas as pd
import numpy as np s = pd.Series(np.random.randn(4))
print ("The original series is:")
print(s)
print('\n') print ("The first two rows of the data series:")
print(s.head(2))
print('\n') print ("The last two rows of the data series:")
print(s.tail(2))
输出结果:
The original series is:
0 0.720876
1 -0.765898
2 0.479221
3 -0.139547
dtype: float64 The first two rows of the data series:
0 0.720876
1 -0.765898
dtype: float64
The last two rows of the data series:2 0.479221
3 -0.139547
dtype: float64
二、DataFrame基本功能
下面来看看数据帧(DataFrame)的基本功能有哪些?下表列出了DataFrame基本功能的重要属性或方法。
编号 | 属性或方法 | 描述 |
---|---|---|
1 | T |
转置行和列。 |
2 | axes |
返回一个列,行轴标签和列轴标签作为唯一的成员。 |
3 | dtypes |
返回此对象中的数据类型(dtypes )。 |
4 | empty |
如果NDFrame 完全为空[无项目],则返回为True ; 如果任何轴的长度为0 。 |
5 | ndim |
轴/数组维度大小。 |
6 | shape |
返回表示DataFrame 的维度的元组。 |
7 | size |
NDFrame 中的元素数。 |
8 | values |
NDFrame的Numpy表示。 |
9 | head() |
返回开头前n 行。 |
10 | tail() |
返回最后n 行。 |
下面来看看如何创建一个DataFrame并使用上述属性和方法。
import pandas as pd d = {'Name':pd.Series(['Tom','James','Ricky','Vin','Steve','Minsu','Jack']),
'Age':pd.Series([25,26,25,23,30,29,23]),
'Rating':pd.Series([4.23,3.24,3.98,2.56,3.20,4.6,3.8])} df = pd.DataFrame(d)
print ("Our data series is:")
print(df)
输出结果:
Our data series is:
Age Name Rating
0 25 Tom 4.23
1 26 James 3.24
2 25 Ricky 3.98
3 23 Vin 2.56
4 30 Steve 3.20
5 29 Minsu 4.60
6 23 Jack 3.80
T(转置)示例
返回DataFrame
的转置。行和列将交换。
import pandas as pd d = {'Name':pd.Series(['Tom','James','Ricky','Vin','Steve','Minsu','Jack']),
'Age':pd.Series([25,26,25,23,30,29,23]),
'Rating':pd.Series([4.23,3.24,3.98,2.56,3.20,4.6,3.8])} df = pd.DataFrame(d)
print ("The transpose of the data series is:")
print(df.T)
输出结果:
The transpose of the data series is:
0 1 2 3 4 5 6
Age 25 26 25 23 30 29 23
Name Tom James Ricky Vin Steve Minsu Jack
Rating 4.23 3.24 3.98 2.56 3.2 4.6 3.8
axes示例
返回行轴标签和列轴标签列表。
import pandas as pd d = {'Name':pd.Series(['Tom','James','Ricky','Vin','Steve','Minsu','Jack']),
'Age':pd.Series([25,26,25,23,30,29,23]),
'Rating':pd.Series([4.23,3.24,3.98,2.56,3.20,4.6,3.8])} df = pd.DataFrame(d)
print ("Row axis labels and column axis labels are:")
print(df.axes)
输出结果:
Row axis labels and column axis labels are:
[RangeIndex(start=0, stop=7, step=1), Index([u'Age', u'Name', u'Rating'],
dtype='object')]
dtypes示例
返回每列的数据类型。
import pandas as pd d = {'Name':pd.Series(['Tom','James','Ricky','Vin','Steve','Minsu','Jack']),
'Age':pd.Series([25,26,25,23,30,29,23]),
'Rating':pd.Series([4.23,3.24,3.98,2.56,3.20,4.6,3.8])} df = pd.DataFrame(d)
print ("The data types of each column are:")
print(df.dtypes)
输出结果:
The data types of each column are:
Age int64
Name object
Rating float64
dtype: object
empty示例
返回布尔值,表示对象是否为空; 返回True
表示对象为空。
import pandas as pd d = {'Name':pd.Series(['Tom','James','Ricky','Vin','Steve','Minsu','Jack']),
'Age':pd.Series([25,26,25,23,30,29,23]),
'Rating':pd.Series([4.23,3.24,3.98,2.56,3.20,4.6,3.8])} df = pd.DataFrame(d)
print ("Is the object empty?",df.empty)
输出结果:
Is the object empty? False
ndim示例
返回对象的维数。根据定义,DataFrame是一个2D
对象。
import pandas as pd d = {'Name':pd.Series(['Tom','James','Ricky','Vin','Steve','Minsu','Jack']),
'Age':pd.Series([25,26,25,23,30,29,23]),
'Rating':pd.Series([4.23,3.24,3.98,2.56,3.20,4.6,3.8])} df = pd.DataFrame(d)
print ("Our object is:")
print(df)
print('\n') print ("The dimension of the object is:",df.ndim)
输出结果:
Our object is:
Age Name Rating
0 25 Tom 4.23
1 26 James 3.24
2 25 Ricky 3.98
3 23 Vin 2.56
4 30 Steve 3.20
5 29 Minsu 4.60
6 23 Jack 3.80
The dimension of the object is:2
shape示例
返回表示DataFrame
的维度的元组。 元组(a,b)
,其中a
表示行数,b
表示列数。
import pandas as pd d = {'Name':pd.Series(['Tom','James','Ricky','Vin','Steve','Minsu','Jack']),
'Age':pd.Series([25,26,25,23,30,29,23]),
'Rating':pd.Series([4.23,3.24,3.98,2.56,3.20,4.6,3.8])} df = pd.DataFrame(d)
print ("Our object is:")
print(df)
print('\n') print ("The shape of the object is:",df.shape)
输出结果:
Our object is:
Age Name Rating
0 25 Tom 4.23
1 26 James 3.24
2 25 Ricky 3.98
3 23 Vin 2.56
4 30 Steve 3.20
5 29 Minsu 4.60
6 23 Jack 3.80
The shape of the object is:(7, 3)
size示例
返回DataFrame中的元素数。
import pandas as pd d = {'Name':pd.Series(['Tom','James','Ricky','Vin','Steve','Minsu','Jack']),
'Age':pd.Series([25,26,25,23,30,29,23]),
'Rating':pd.Series([4.23,3.24,3.98,2.56,3.20,4.6,3.8])} df = pd.DataFrame(d)
print ("Our object is:")
print(df)
print('\n') print ("The total number of elements in our object is:",df.size)
输出结果:
Our object is:
Age Name Rating
0 25 Tom 4.23
1 26 James 3.24
2 25 Ricky 3.98
3 23 Vin 2.56
4 30 Steve 3.20
5 29 Minsu 4.60
6 23 Jack 3.80
The total number of elements in our object is:21
values示例
将DataFrame
中的实际数据作为ndarray
返回。
import pandas as pd d = {'Name':pd.Series(['Tom','James','Ricky','Vin','Steve','Minsu','Jack']),
'Age':pd.Series([25,26,25,23,30,29,23]),
'Rating':pd.Series([4.23,3.24,3.98,2.56,3.20,4.6,3.8])} df = pd.DataFrame(d)
print ("Our object is:")
print(df)
print('\n')
print ("The actual data in our data frame is:")
print(df.values)
输出结果:
Our object is:
Age Name Rating
0 25 Tom 4.23
1 26 James 3.24
2 25 Ricky 3.98
3 23 Vin 2.56
4 30 Steve 3.20
5 29 Minsu 4.60
6 23 Jack 3.80
The actual data in our data frame is:
[[25 'Tom' 4.23]
[26 'James' 3.24]
[25 'Ricky' 3.98]
[23 'Vin' 2.56]
[30 'Steve' 3.2]
[29 'Minsu' 4.6]
[23 'Jack' 3.8]]
head()和tail()示例
要查看DataFrame对象的小样本,可使用head()
和tail()
方法。
head()
返回前n
行(观察索引值)。显示元素的默认数量为5
,但可以传递自定义数字值。
tail()
返回最后n
行(观察索引值)。显示元素的默认数量为5
,但可以传递自定义数字值。
import pandas as pd d = {'Name':pd.Series(['Tom','James','Ricky','Vin','Steve','Minsu','Jack']),
'Age':pd.Series([25,26,25,23,30,29,23]),
'Rating':pd.Series([4.23,3.24,3.98,2.56,3.20,4.6,3.8])} df = pd.DataFrame(d)
print ("Our data frame is:")
print(df)
print('\n') print ("The first two rows of the data frame is:")
print(df.head(2))
print('\n') print ("The last two rows of the data frame is:")
print(df.tail(2))
输出结果:
Our data frame is:
Age Name Rating
0 25 Tom 4.23
1 26 James 3.24
2 25 Ricky 3.98
3 23 Vin 2.56
4 30 Steve 3.20
5 29 Minsu 4.60
6 23 Jack 3.80
The first two rows of the data frame is:
Age Name Rating
0 25 Tom 4.23
1 26 James 3.24
The last two rows of the data frame is:
Age Name Rating
5 29 Minsu 4.6
6 23 Jack 3.8
Pandas | 05 基本功能的更多相关文章
- pandas的基本功能(一)
第16天pandas的基本功能(一) 灵活的二进制操作 体现在2个方面 支持一维和二维之间的广播 支持缺失值数据处理 四则运算支持广播 +add - sub *mul /div divmod()分区和 ...
- pandas的筛选功能,跟excel的筛选功能类似,但是功能更强大。
Select rows from a DataFrame based on values in a column -pandas 筛选 https://stackoverflow.com/questi ...
- (数据科学学习手札134)pyjanitor:为pandas补充更多功能
本文示例代码及文件已上传至我的Github仓库https://github.com/CNFeffery/DataScienceStudyNotes 1 简介 pandas发展了如此多年,所包含的功能已 ...
- python数据分析之Pandas:基本功能介绍
Pandas有两个主要的数据结构:Series和DataFrame. Series是一种类似于一维数组的对象,它由一组数据以及一组与之相关的数据标签构成.来看下它的使用过程 In [1]: from ...
- Pandas常用基本功能
Series 和 DataFrame还未构建完成的朋友可以参考我的上一篇博文:https://www.cnblogs.com/zry-yt/p/11794941.html 当我们构建好了 Series ...
- Pandas | 21 日期功能
日期功能扩展了时间序列,在财务数据分析中起主要作用.在处理日期数据的同时,我们经常会遇到以下情况 - 生成日期序列 将日期序列转换为不同的频率 创建一个日期范围 通过指定周期和频率,使用date.ra ...
- pandas replace 替换功能function
list like replace method dict like replace method regex expression import pandas as pd import numpy ...
- [Pandas] 05 - Parallel processing
相关资源 [Python] 09 - Multi-processing [Pandas] 01 - A guy based on NumPy [AI] 深度数学 - Bayes 这章非常有意思,但一定 ...
- 3.1,pandas【基本功能】
一:改变索引 reindex方法对于Series直接索引,对于DataFrame既可以改变行索引,也可以改变列索引,还可以两个一起改变. 1)对于Series In [2]: seri = pd.Se ...
随机推荐
- I/O多路复用-EPOLL探索
什么是I/O多路复用 I/O多路复用就是通过一种机制,可以监视多个描述符,一旦某个IO能够读写,通知程序进行相应的读写操作. I/O多路复用的场合 1.当客户处理多个描述字时(通常是交互式输入和网络套 ...
- jmeter 如何获取一小时之前的时间戳
正确答案: ${__intSum(${__time(/1000,)},-3600,)} 如果还要显示毫秒 ${__longSum(${__time},-3600000,)}
- spring好文章整理
彻底搞明白Spring中的自动装配和Autowired IDEA编译spring 5源码 Spring源码——IDEA读Spring源码环境搭建 导入spring源码org.springframewo ...
- layui提示框事件
使用layui提示框的时候遇到个问题,点击“确定”“取消”之类的按钮会执行里面的方法,但点击×就不会执行,所以在添加数据的时候出现个BUG,就是保存数据后点击弹出提示框的×,继续保存,如此循环,就可以 ...
- mvc5 源码解析2-2 mvchandler的执行
我们从application获取的时候查看stepmanager的实现类 IHttpHandler applicationInstance = HttpApplicationFactory.GetAp ...
- 处理 unassigned shard
#查看所有分片 GET _cat/shards curl 10.1.2.2:9200/_cat/indices/iis_log* #查看索引的分片状态 #查看第一个unassigned shard的 ...
- topshelf注册服务
1.需要从nutget上获取toshelf配置 2.代码 using Common.Logging; using Quartz; using Quartz.Impl; using System; us ...
- 开发--Deepin系统安装
开发|Deepin系统安装 在18小时前,我刚刚萌生了一个将我的笔记本换成linux系统.在18小时后的现在,在我各种试错之后,笔记本已经开始跑起linux了.在科技的时代,只要是想法,都可以试一试. ...
- JS 树形结构与数组结构相互转换、在树形结构中查找对象
总是有很多需求是关于处理树形结构的,所以不得不总结几个常见操作的写法.¯\_(ツ)_/¯ 首先假设有一个树形结构数据如下 var tree=[ { 'id': '1', 'name': '教学素材管理 ...
- flink Iterate迭代基本概念
基本概念:在流中创建“反馈(feedback)”循环,通过将一个算子的输出重定向到某个先前的算子.这对于定义不断更新模型的算法特别有用. 迭代的数据流向:DataStream → IterativeS ...