Series基础

Series创建

import numpy as np
import pandas as pd
from pandas import Series,DataFrame

两种创建方式:

由列表或numpy数组创建

默认索引为0到N-1的整数型索引

s = Series([1,2,3])

nd = np.array([1,2,3])
s = Series(nd)

#通过index参数指定索引
s = Series([1,2,3,4],index=['a','b','c','d'])

由字典创建

s = Series({'a':1,'b':2})

Series索引和切片

#loc 都是左闭右闭   iloc都是左闭右开

显式索引:(使用.loc[](推荐))

#左闭右闭
s["pi"]			s.loc["a"]
#float
s[["a","pi"]]		s.loc[["a"]]

s.loc["a":"g"]
#左闭右闭
s.iloc[0:2]
#左闭右开

隐式索引:(使用.iloc[](推荐))

#左闭右开		[行数]

s[0]
s.iloc[0]
s.iloc[[1,2]]
s.iloc[0:2]

Series的基本概念

series的属性

	s.shape		s.index
	s.size		s.values

Series的样式 head(),tail()

s.head(2)
s.tail(2)

检测缺失数据

#重点
	pd.isnull(s)
#返回的是boolean   如果为空  true  不为空 false
    ind = s.isnull()
    ind
#获取了数据是否为空, 进行检索,如果数据为空,就赋值
    s[ind] = 1000   

name属性

s.name = "Python"

Series的运算

适用于numpy的数组运算也适用于Series

Series之间的运算

A = pd.Series([2,4,6],index=[0,1,2])
B = pd.Series([1,3,5],index=[1,2,3])
display(A,B)

在运算中自动对齐不同索引的数据
如果索引不对应,则补NaN
注意:要想保留所有的index,则需要使用.add()函数

s3 = s1.add(s, fill_value=0)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值