脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - pandas 数据结构之Series的使用方法

pandas 数据结构之Series的使用方法

2021-07-17 00:36Amei1314 Python

这篇文章主要介绍了pandas 数据结构之Series的使用方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

1. series

series 是一个类数组的数据结构,同时带有标签(lable)或者说索引(index)。

1.1 下边生成一个最简单的series对象,因为没有给series指定索引,所以此时会使用默认索引(从0到n-1)。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
# 引入series和dataframe
in [16]: from pandas import series,dataframe
in [17]: import pandas as pd
 
in [18]: ser1 = series([1,2,3,4])
 
in [19]: ser1
out[19]:
0  1
1  2
2  3
3  4
dtype: int64

1.2 当要生成一个指定索引的series 时候,可以这样:  

?
1
2
3
4
5
6
7
8
9
10
# 给index指定一个list
in [23]: ser2 = series(range(4),index = ["a","b","c","d"])
 
in [24]: ser2
out[24]:
0
1
2
3
dtype: int64

1.3 也可以通过字典来创建series对象

?
1
2
3
4
5
6
7
8
9
10
11
in [45]: sdata = {'ohio': 35000, 'texas': 71000, 'oregon': 16000, 'utah': 5000}
 
in [46]: ser3 = series(sdata)
# 可以发现,用字典创建的series是按index有序的
in [47]: ser3
out[47]:
ohio   35000
oregon  16000
texas   71000
utah    5000
dtype: int64

在用字典生成series的时候,也可以指定索引,当索引中值对应的字典中的值不存在的时候,则此索引的值标记为missing,na,并且可以通过函数(pandas.isnull,pandas.notnull)来确定哪些索引对应的值是没有的。 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
in [48]: states = ['california', 'ohio', 'oregon', 'texas']
 
in [49]: ser3 = series(sdata,index = states)
 
in [50]: ser3
out[50]:
california    nan
ohio     35000.0
oregon    16000.0
texas     71000.0
dtype: float64
# 判断哪些值为空
in [51]: pd.isnull(ser3)
out[51]:
california   true
ohio     false
oregon    false
texas     false
dtype: bool
 
in [52]: pd.notnull(ser3)
out[52]:
california  false
ohio      true
oregon     true
texas     true
dtype: bool

1.4 访问series中的元素和索引:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 访问索引为"a"的元素
in [25]: ser2["a"]
out[25]: 0
# 访问索引为"a","c"的元素
in [26]: ser2[["a","c"]]
out[26]:
0
2
dtype: int64
# 获取所有的值
in [27]: ser2.values
out[27]: array([0, 1, 2, 3])
# 获取所有的索引
in [28]: ser2.index
out[28]: index([u'a', u'b', u'c', u'd'], dtype='object')

1.5 简单运算

在pandas的series中,会保留numpy的数组操作(用布尔数组过滤数据,标量乘法,以及使用数学函数),并同时保持引用的使用

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
in [34]: ser2[ser2 > 2]
out[34]:
64
d   3
dtype: int64
 
in [35]: ser2 * 2
out[35]:
128
b   2
c   4
d   6
dtype: int64
 
in [36]: np.exp(ser2)
out[36]:
6.235149e+27
2.718282e+00
7.389056e+00
2.008554e+01
dtype: float64

1.6 series的自动对齐

series的一个重要功能就是自动对齐(不明觉厉),看看例子就明白了。 差不多就是不同series对象运算的时候根据其索引进行匹配计算。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# ser3 的内容
in [60]: ser3
out[60]:
ohio   35000
oregon  16000
texas   71000
utah    5000
dtype: int64
# ser4 的内容
in [61]: ser4
out[61]:
california    nan
ohio     35000.0
oregon    16000.0
texas     71000.0
dtype: float64
# 相同索引值的元素相加
in [62]: ser3 + ser4
out[62]:
california     nan
ohio      70000.0
oregon     32000.0
texas     142000.0
utah        nan
dtype: float64

1.7 命名

series对象本身,以及索引都有一个 name 属性

?
1
2
3
4
5
6
7
8
9
10
11
12
in [64]: ser4.index.name = "state"
 
in [65]: ser4.name = "population"
 
in [66]: ser4
out[66]:
state
california    nan
ohio     35000.0
oregon    16000.0
texas     71000.0
name: population, dtype: float64

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://www.cnblogs.com/linux-wangkun/p/5903380.html

延伸 · 阅读

精彩推荐