目录
1.4 用numpy数组或其他series对象定义新series对象
0. 回顾:
之前我们学习了numpy库,这一节我们将学习数据分析的核心库-pandas库。
1. Series对象
pandas中最主要有两种格式,一种是Series数据结构,另一种是DataFrame数据结构。Series数据结构跟数组相似,它的内部结构很简单,由两个相互关联的数组组成。其中,主数用来存放数据,主数的每个元素都有一个与之相关的标签,这些标签存放在index数组中。
1.1 声明Series对象
利用series()函数来构造,将数组进行输入。
示例1:
import numpy as np
import pandas as pd
a = np.arange(3, 7)
s = pd.Series(a)
print(s)
运行结果为:
0 3
1 4
2 5
3 6
dtype: int32
可以看到,此时的s就是一个series结构,其中最左边一列为标签,右边为对应的元素,默认是从0开始。当然,也可以使用带有意义的标签。
示例2:
import pandas as pd
s = pd.Series([3, 4, 6], index=['a', 'b', 'c'])
print(s)
运行结果为:
a 3
b 4
c 6
dtype: int64
想要查看这两个数组,则可以调用相关的属性即可。
示例3:
import pandas as pd
s = pd.Series([3, 4, 6], index=['a', 'b', 'c'])
print(s)
print(s.index)
print(s.values)
运行结果为:
a 3
b 4
c 6
dtype: int64
Index(['a', 'b', 'c'], dtype='object')
[3 4 6]
1.2 选择内部元素
获取其内部的元素,只需当成普通的数组引用或者指定相应的键即可。
示例4:
import pandas as pd
s = pd.Series([3, 4, 6], index=['a', 'b', 'c'])
print(s)
print(s[0])
print(s.iloc[0:2])
print(s[['b', 'c']])
运行结果为:
a 3
b 4
c 6
dtype: int64
3
a 3
b 4
dtype: int64
b 4
c 6
dtype: int64
值得注意的是,现在可以直接用s[0]来进行索引,但在未来的版本,直接跟[ ]会被识别成标签类,故要使用索引,最好使用iloc来表示。
1.3 为元素赋值
赋值也是不言而喻了。
示例5:
import pandas as pd
s = pd.Series([3, 4, 6], index=['a', 'b', 'c'])
print(s)
s.iloc[1:2] = 5
s['c'] = 7
print(s)
运行结果为:
a 3
b 4
c 6
dtype: int64
a 3
b 5
c 7
dtype: int64
1.4 用numpy数组或其他series对象定义新series对象
用这种方法定义的是原数组的一个视图,并不是其副本,改变原来的数组新的也会改变。
示例6:
import numpy as np
import pandas as pd
s = np.arange(3, 5)
s1 = pd.Series(s)
s2 = pd.Series(s1)
print(s1)
print(s2)
s[0] = 4
print(s1)
print(s2)
运行结果为:
0 3
1 4
dtype: int32
0 3
1 4
dtype: int32
0 4
1 4
dtype: int32
0 4
1 4
dtype: int32
可以看到,改变了最初的数组,s1和s2都会发生改变。
1.5 筛选对象
可以根据调剂进行筛选对象。
示例7:
import numpy as np
import pandas as pd
s = np.arange(3, 7)
s1 = pd.Series(s)
print(s1)
print(s1[s1 > 5])
运行结果为:
0 3
1 4
2 5
3 6
dtype: int32
3 6
dtype: int32
1.6 series对象运算和数学函数
可以直接利用Python中数算术符进行运算,也可以使用numpy中的各种函数。
示例8:
import numpy as np
import pandas as pd
s = np.arange(3, 7)
s1 = pd.Series(s)
print(s1 / 2)
print(np.log(s1))
运行结果为:
0 1.5
1 2.0
2 2.5
3 3.0
dtype: float64
0 1.098612
1 1.386294
2 1.609438
3 1.791759
dtype: float64
1.7 series对象的组成元素
当series对象中有重复元素的时候,可以用unique()函数去除重复的元素。
示例9:
import numpy as np
import pandas as pd
s = np.array([1, 2, 3, 2, 1])
print(s)
print(pd.unique(s))
运行结果为:
[1 2 3 2 1]
[1 2 3]
利用value_counts()函数来统计元素出现的次数。
示例10:
import pandas as pd
s = pd.Series([1, 2, 3, 2, 1])
print(s)
print(s.value_counts())
运行结果为:
0 1
1 2
2 3
3 2
4 1
dtype: int64
1 2
2 2
3 1
Name: count, dtype: int64
利用isin()来判断所属元素是否属于其中。
示例11:
import pandas as pd
s = pd.Series([1, 2, 3, 2, 1])
print(s)
print(s.isin([1, 2]))
print(s[s.isin([1, 2])])
运行结果为:
0 1
1 2
2 3
3 2
4 1
dtype: int64
0 True
1 True
2 False
3 True
4 True
dtype: bool
0 1
1 2
3 2
4 1
dtype: int64
1.8 NaN
当出现NaN格式的元素时,即表示这个元素出现问题,如缺失或者计算错误,但允许出现这个类型的元素。
示例12:
import numpy as np
import pandas as pd
s = pd.Series([1, 2, np.NAN, 2, 1])
print(s)
运行结果为:
0 1.0
1 2.0
2 NaN
3 2.0
4 1.0
dtype: float64
可以利用isnull()函数和notnull()函数来筛选是否为NaN。
示例13:
import numpy as np
import pandas as pd
s = pd.Series([1, 2, np.NAN, 2, 1])
print(s)
print(s.isnull())
print(s.notnull())
运行结果为:
0 1.0
1 2.0
2 NaN
3 2.0
4 1.0
dtype: float64
0 False
1 False
2 True
3 False
4 False
dtype: bool
0 True
1 True
2 False
3 True
4 True
dtype: bool
1.9 series用作字典
可以把series对象当做字典来用。
示例14:
import pandas as pd
mydict = {'red': 2, 'blue': 4, 'yellow': 6}
myseries1 = pd.Series(mydict)
print(myseries1)
color = ['red', 'blue', 'yellow', 'orange']
myseries2 = pd.Series(mydict, index=color)
print(myseries2)
运行结果为:
red 2
blue 4
yellow 6
dtype: int64
red 2.0
blue 4.0
yellow 6.0
orange NaN
dtype: float64
可以看到,当标签出现多的时,会使其值为NaN。
1.10 series对象之间的运算
两个具有不同标签的对象也可以进行运算,其只对具有相同标签的进行运算。
示例15:
import pandas as pd
mydict1 = {'red': 2, 'blue': 4, 'yellow': 6}
myseries1 = pd.Series(mydict1)
mydict2 = {'red': 2, 'blue': 4, 'orange': 6}
myseries2 = pd.Series(mydict2)
print(myseries1 + myseries2)
运行结果为:
blue 8.0
orange NaN
red 4.0
yellow NaN
dtype: float64