pandas 入门之Series学习

pandas 入门学习

Series 使用方法:

  • Series定义
  • Series 对象基本创建,值,索引如何访问
  • Series 对象通过字典创建
  • 判断Series对象是否存在缺失值
import pandas as pd
from pandas import Series, DataFrame
import numpy as np

Series 是一种类似于一维数组的对象,它是由一组数据(各种numpy数据)以及一组与之相关的索引组成

obj = Series([1,2,3,4,5])
obj
0    1
1    2
2    3
3    4
4    5
dtype: int64
obj.values   #可以通过values访问对象的值
array([1, 2, 3, 4, 5], dtype=int64)
obj.index # 可以通过index 查看对象的索引
RangeIndex(start=0, stop=5, step=1)
obj = Series([1,2,3,4,5],index=['a','b','c','d','e']) # 指明索引
obj
a    1
b    2
c    3
d    4
e    5
dtype: int64
obj.index
Index(['a', 'b', 'c', 'd', 'e'], dtype='object')
obj.values
array([1, 2, 3, 4, 5], dtype=int64)
obj['a']
1
obj[['a','b']] # 访问通过索引访问值
a    1
b    2
dtype: int64
obj[obj > 2]
c    3
d    4
e    5
dtype: int64
obj.values*2
array([ 2,  4,  6,  8, 10], dtype=int64)
obj*2
a     2
b     4
c     6
d     8
e    10
dtype: int64
np.exp(obj)
a      2.718282
b      7.389056
c     20.085537
d     54.598150
e    148.413159
dtype: float64
obj
a    1
b    2
c    3
d    4
e    5
dtype: int64

直接使用字典创建Series对象

dic = {'a':1, 'b':2, "c":3, 'd':4}
df = Series(dic) # 对象的索引可以指定d但是需要和字典关键字一样,不指定的话就是字典的关键字
df
a    1
b    2
c    3
d    4
dtype: int64
idx = ['w','x','b','a']
df = Series(dic, index=idx) # 指定索引和字典的关键字求交集,如果没有交集则为nan
df
w    NaN
x    NaN
b    2.0
a    1.0
dtype: float64

如何处理确实数据:

检测是否存在缺失数据 isnull(), notnull()函数

df.isnull()# w,x存在缺失值
w     True
x     True
b    False
a    False
dtype: bool
df.notnull()
w    False
x    False
b     True
a     True
dtype: bool

Series 最重要的一个功能就是:它在算术运算时会自动对齐不同索引的数据(不懂这句话等深入看了再解释吧)

df.name = 'values' # 对象本身和索引的name属性
df.index.name = 'zimu'
df
zimu
w    NaN
x    NaN
b    2.0
a    1.0
Name: values, dtype: float64
df.index = ['L','S','P','P'] # 索引可以修改
df
L    NaN
S    NaN
P    2.0
P    1.0
Name: values, dtype: float64
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值