Pandas_1:Pandas中Series类

Pandas介绍

  • 经常和别的工具一起使用,特别是Scipy,Numpy以及Matplotlib
  • 支持Numpy风格的的数组计算
  • Pandas适合处理表格/异质型的数据,但Numpy适合处理同质型的

Pandas数据结构介绍

Series类型

以为数组型对象,包含一个:值序列,一个数据标签(index:索引)

如果不指定,默认索引就是从0到N-1

通过valuesindex分别获得值与索引

import pandas as pd
obj = pd.Series([4, 7, -5, 3])
obj
0    4
1    7
2   -5
3    3
dtype: int64

print(obj.values)
print(obj.index)

通常需要特定指定索引的
obj2 = pd.Series([4, 7, -5, 3], index = ['d', 'b', 'a', 'c'])
obj2
d    4
b    7
a   -5
c    3
dtype: int64
obj2.index
Index(['d', 'b', 'a', 'c'], dtype='object')

Series的一些性质

  • 可以直接索引
  • 可以通过索引更改值
  • 可以使用神奇索引,以及布尔索引
  • 与标量相乘
  • obj放在numpy数学函数里也可以
print(obj2['b'])
7
# 当然也可以通过索引更改值
obj2['d'] = 6
# 也可以使用神奇索引
obj2[['c', 'a', 'd']]
c    3
a   -5
d    6
dtype: int64
# 这里直接写obj2>0也可以,效果是一样的
obj2[obj2.values > 0]
d    6
b    7
c    3
dtype: int64
# 用于numpy数学函数
import numpy as np
np.exp(obj2)
d     403.428793
b    1096.633158
a       0.006738
c      20.085537
dtype: float64

可以将Series类型看作是:长度固定且有序的字典

'b' in obj2
True
'r' in obj2
False

用字典生成一个Series

sdata = {'Ohio': 35000, 'Texas': 71000, 'Oregon': 16000, 'Utah': 5000}
obj3 = pd.Series(sdata)
obj3
Ohio      35000
Texas     71000
Oregon    16000
Utah       5000
dtype: int64

没有index的那个值就用NaN(Not a Number)来替代,NA是pandas中替代缺失值的元素

states = ['California', 'Ohio', 'Oregon', 'Texas']
obj4 = pd.Series(sdata, index = states)
obj4
California        NaN
Ohio          35000.0
Oregon        16000.0
Texas         71000.0
dtype: float64

使用isnull/notnull来检测缺失值

pd.isnull(obj4)
California     True
Ohio          False
Oregon        False
Texas         False
dtype: bool
pd.notnull(obj4)
California    False
Ohio           True
Oregon         True
Texas          True
dtype: bool

自动对齐是一个非常好用的东西

obj3 + obj4
California         NaN
Ohio           70000.0
Oregon         32000.0
Texas         142000.0
Utah               NaN
dtype: float64

pd.Series()数据类型的对象本身和其中的index都有name属性

obj3.index.name = 'state'
obj3.name = 'population'
obj3
state
Ohio      35000
Texas     71000
Oregon    16000
Utah       5000
Name: population, dtype: int64

可以按照位置索引赋值

obj
0    4
1    7
2   -5
3    3
dtype: int64
obj.index = ['Bob', 'Steve', 'Jeff', 'Ryan']
obj
Bob      4
Steve    7
Jeff    -5
Ryan     3
dtype: int64
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值