padans之Series入坑之门

一 前言

本篇文章是pandas的Series基础入门文章,pandas是一个强大的数据分析工具,其核心就是 Series 存储一维数据,DataFram储存多维数据;使用pandas的好处就是可以节省许多代码,不必写许多数据分析函数;疫情情况下,工作不容易,每天还要为吃饭奔波,成年人的世界就没有容易二字,希望读者珍惜当下来之不易的生活;有时候一个人真的很酷,但也很苦;

二打印结构

pandas的库安装建议读者使用anaconda安装,会节省很多力气,后续的许多库都是使用anaconda创建的虚拟环境;要不然使用原生的pip会出现各种问题;

2.1 查看Series结构

Series的结构是是两个并列的数组,左边是索引index,右边是value;当将一个数组传进Series对象之后就会自动创建索引,索引的起始位置为0;

# -*- coding: utf-8 -*-

import pandas as pd
import numpy as np


# 打印 ser
ser = pd.Series([1,2,3,4])
print(ser)

输出

0    1
1    2
2    3
3    4
dtype: int64

2.2 获得所有值

单独的获得Series值可以使用values属性

ser = pd.Series([1,2,3,4], ['a', 'b', 'c', 'd'])
print(ser.values)

输出

[1 2 3 4]

2.3 获得所有索引

虽然Series有默认的索引,但有时候应为需求,我们需要自定义索引(像字典一样,有key,value,通过key获得value)就可以使用如下方式,默认第一个参数是值,第二个参数是索引;单独的获得索引可以使用index属性

ser = pd.Series([1,2,3,4], ['a', 'b', 'c', 'd'])
print(ser.index)

输出

Index(['a', 'b', 'c', 'd'], dtype='object')

三取值切片

3.1 指定取值

Series的取值类似数组,如下所示索引0和自定义索引a都指向值1

# val - index
ser = pd.Series([1,2,3,4], ['a', 'b', 'c', 'd'])
# 指定索引后,按数字索引与指定索引一致
print(ser[0])
print(ser['a'])

输出

1
1

3.2 切片取值

Series的切片类似数组, 如下所示, 切片为 0~2(不包括2)

ser = pd.Series([1,2,3,4], ['a', 'b', 'c', 'd'])
# 切片
print(ser[0:2])

输出

a    1
b    2
dtype: int64

3.3 可选取值

除了简单的取值,还可以通过指定多个的索引进行取值,如下所示 指定索引 a,c 为一个数组作为参数;

# val - index
ser = pd.Series([1,2,3,4], ['a', 'b', 'c', 'd'])
print(ser[['a','c']])

输出

a    1
c    3
dtype: int64

四赋值操作

Series赋值操作与数组一致,在对应的元数位置赋值即可;

ser = pd.Series([1,2,3,4], ['a', 'b', 'c', 'd'])
ser[0] = 5
ser[1] = 6
print(ser)

输出

a    5
b    6
c    3
d    4
dtype: int64

五创建方式

5.1 numpy数字创建方式

简单的使用numpy 创建一个数组,将其作为参数放进Series对象即可;这种形式有个缺点就是如果numpy创建的数组值变了,Series的值也会随之改变,类似numpy的视图思路;

arr = np.array([1,2,3,4])
ser = pd.Series(arr)

输出

0    1
1    2
2    3
3    4
dtype: int32

5.2 字典方式

除了通过简单的使用数组入参,还可以使用字典的方式,这样就将key转为了Series的index,value作为Series的值

dic = {'a':'100','b':'101'}
ser = pd.Series(dic)
print(ser)

输出

a    100
b    101
dtype: object

六 算术运算

Series 的算术运算支持 + , - * / 等各种数字函数,类似numpy的算术运算;下面的示例中每次运算都是元素级别操作

3.1 乘法

ser = pd.Series([1,2,3,4])
ser_multiply = ser * 2
print(ser_multiply)

输出

0    2
1    4
2    6
3    8
dtype: int64

6.2 加法

ser = pd.Series([1,2,3,4])
ser_add = ser + 2
print(ser_add)

输出

0    3
1    4
2    5
3    6
dtype: int64

6.3 除法

ser = pd.Series([1,2,3,4])
ser_div = ser / 2
print(ser_div)

输出

0    0.5
1    1.0
2    1.5
3    2.0
dtype: float64

6.4 减法

ser = pd.Series([1,2,3,4])
ser_sub = ser - 2
print(ser_sub)

输出

0   -1
1    0
2    1
3    2
dtype: int64

七 简单筛选

7.1 去重

去重筛选可以使用 unique 函数,类似 SQL中 distinct关键字;

ser = pd.Series([0,1,2,3,4,0,1,2,3,4])
print(ser.unique())

输出

[0 1 2 3 4]

7.2 统计个数

统计个数使用 value_counts 函数,会统计每个元素出现的个数,当然是具有去重效果;

ser = pd.Series([0,1,2,3,4,0,1,2,3,4])
print(ser.value_counts())
4    2
3    2
2    2
1    2
0    2
dtype: int64

7.3 是否存在

判断Series 是否存在某个元数 可以使用 isin ,参数为一个数组,可以进行多个判断;如下所示判断Series是否存在 0 或者 1

ser = pd.Series([0,1,2,3,4,0,1,2,3,4])
print(ser.isin([0,1]))

输出

0     True
1     True
2    False
3    False
4    False
5     True
6     True
7    False
8    False
9    False
dtype: bool

八 Nan

Nan在数字类型中一般代表非数字类型数字,或者超出指定数据类型的范围;

8.1 创建带有Nan的Serial

创建一个带有Nan的 Series 可以使用 numpy.NaN;

ser = pd.Series([0,1,2,3,np.NaN,4,5])
print(ser)

输出

0    0.0
1    1.0
2    2.0
3    3.0
4    NaN
5    4.0
6    5.0
dtype: float64

8.2 空判定

判断一个Series是否存在空值 即 NaN 的情况,可以使用 isnull() 函数,返回的是布尔类型

ser = pd.Series([0,1,2,3,np.NaN,4,5])
print(ser.isnull())

输出

0    False
1    False
2    False
3    False
4     True
5    False
6    False
dtype: bool

还可以将 isnull() 的结果作为过滤条件筛选出Nan的index

ser = pd.Series([0,1,2,3,np.NaN,4,5])
# 筛选空
print(ser[ser.isnull()])

输出

4   NaN
dtype: float64

8.3 非空判断

有了空判断就有非空判断,使用notnull() 函数就可以达到目的,返回的同样是布尔类型

ser = pd.Series([0,1,2,3,np.NaN,4,5])
print(ser.notnull())

输出

0     True
1     True
2     True
3     True
4    False
5     True
6     True
dtype: bool

notnull()的结果作为筛选条件,就会输出全是非空的indx 和对应的value

ser = pd.Series([0,1,2,3,np.NaN,4,5])
# 筛选非空
print(ser[ser.notnull()])

输出

0    0.0
1    1.0
2    2.0
3    3.0
5    4.0
6    5.0
dtype: float64

九 name属性

9.1 Series对象的name属性

Series 对象的有个name属性,可以通过 赋值或者入参的形式指定;

ser = pd.Series([0,1,2,3,np.NaN,4,5])
ser.name = 'zszxz'
print(ser)

输出

0    0.0
1    1.0
2    2.0
3    3.0
4    NaN
5    4.0
6    5.0
Name: zszxz, dtype: float64

9.2 Series 的 index 的name 属性

注意与Series对象的name属性区分开,这个Series 对象索引的name属性;

ser = pd.Series([0,1,2,3,np.NaN,4,5])
ser.index.name = 'zszxz'
print(ser)

输出

zszxz
0    0.0
1    1.0
2    2.0
3    3.0
4    NaN
5    4.0
6    5.0
dtype: float64
发布了148 篇原创文章 · 获赞 375 · 访问量 18万+

猜你喜欢

转载自blog.csdn.net/youku1327/article/details/104401831