首页 > 编程语言 > 详细

1-python数据分析-Pandas基础操作

时间:2020-07-13 19:43:26      阅读:53      评论:0      收藏:0      [点我收藏+]

为什么学习pandas

  • numpy已经可以帮助我们进行数据的处理了,那么学习pandas的目的是什么呢?
    • numpy能够帮助我们处理的是数值型的数据,当然在数据分析中除了数值型的数据还有好多其他类型的数据(字符串,时间序列),那么pandas就可以帮我们很好的处理数值型数据!

什么是pandas?

对非数值型的数据进行存储和运算操作

  • 首先先来认识pandas中的两个常用的类
    • Series 一维数组
    • DataFrame  是由Series组成,多组Series组成一个DataFrame, 二维表格

Series

  • Series是一种类似一维数组的容器对象,由下面两个部分组成:
    • values:一组数据(ndarray类型)
    • index:相关的数据索引标签

Series的创建

    • 由列表或numpy数组创建
    • 由字典创建
# 将列表作为数据源
Series(data=[1,2,3,4,5])

0    1
1    2
2    3
3    4
4    5
dtype: int64

# 将字典作为数据源
dic = {
    A: 100,
    B: 99,
    C: 150,
}
Series(data=dic)

A    100
B     99
C    150
dtype: int64

# 将numpy数组作为数据源
Series(np.random.randint(0,100,size=(3,4)))  
# 上面这样会报错,Series只能存一维的数据

Series(np.random.randint(0,100,size=(3,)))  
0    97
1    75
2    78
dtype: int32

Series的索引类型

  • 隐式索引:默认形式的索引(0,1,2....)
  • 显式索引:自定义的索引,可以通过index参数设置显式索引
# 加上index参数就可以将索引设为显式索引

Series(data=np.random.randint(0,100,size=(3,)), index=[A,B,C])
A     3
B    22
C    63
dtype: int32

Series的索引操作和切片操作

s = Series(data=np.linspace(0,30, num=5),index=[a,b,c,d,e])
a     0.0
b     7.5
c    15.0
d    22.5
e    30.0
dtype: float64

# 索引取值
s[1]  # 7.5   # 隐式索引

s[c]  # 15.0  #显式索引

s.d  # 22.5  #显式索引

# 切片
s[0:3]  # 隐式索引
a     0.0
b     7.5
c    15.0
dtype: float64

s[a:d]  #显式索引
a     0.0
b     7.5
c    15.0
d    22.5
dtype: float64

Series的常用属性

  • shape 形状
  • size 元素个数
  • index 索引
  • values 值
s.shape  # 形状
(5,)

s.size  # 元素个数
5

s.index  # 索引    有显式就是显式索引
Index([a, b, c, d, e], dtype=object)

s.values  #
array([ 0. ,  7.5, 15. , 22.5, 30. ])

Series的算术运算

  • 法则:索引一致的元素进行算数运算否则补空
# 索引一致的元素进行算数运算否则补空

s1 = Series(data=[1,2,3,4,5],index=[a,b,c,d,e])
s2 = Series(data=[1,2,3,4,5],index=[a,b,f,d,e])
s = s1+s2

a     2.0
b     4.0
c     NaN
d     8.0
e    10.0
f     NaN
dtype: float64

Series的常用方法

  • head(), tail():显示Series的前n个或者后n个元素
  • unique(), nunique():去除重复元素, 统计去重后的元素个数
  • isnull(), notnull()
  • add() sub() mul() div()
s = Series(data=np.linspace(0,30, num=5),index=[a,b,c,d,e])
a     0.0
b     7.5
c    15.0
d    22.5
e    30.0
dtype: float64

s.head(3)   # 只显示前三个  #不写默认值是5
a     0.0
b     7.5
c    15.0
dtype: float64

s.tail(2)  # 只显示后俩个
d    22.5
e    30.0
dtype: float64

s=Series(data=[1,2,2,4,4,4,4,5,6,3,3,4,5,3,6,1,3])
s.unique()  # 去掉重复元素
array([1, 2, 4, 5, 6, 3], dtype=int64)

s.nunique()  # 统计去重后的元素个数
6



s1 = Series(data=[1,2,3,4,5],index=[a,b,c,d,e])
s2 = Series(data=[1,2,3,4,5],index=[a,b,f,d,e])
s = s1+s2
s
a     2.0
b     4.0
c     NaN
d     8.0
e    10.0
f     NaN
dtype: float64

s.isnull()  # 检测Series哪些元素为空,为空则返回True,否则返回Fasle
a    False
b    False
c     True
d    False
e    False
f     True
dtype: bool

s.notnull()  # 检测Series哪些元素非空,非空则返回True,否则返回Fasle
a     True
b     True
c    False
d     True
e     True
f    False
dtype: bool

# 想要将s中的非空的数据取出
s[[True,True,False,True,True,False]]   #布尔值可以作为索引去取值
a     2.0
b     4.0
d     8.0
e    10.0
dtype: float64

# 直接将s.notnull()得到的布尔值作为索引取值,可以对Series中空值进行清洗
s[s.notnull()]  
a     2.0
b     4.0
d     8.0
e    10.0
dtype: float64

 

 

1-python数据分析-Pandas基础操作

原文:https://www.cnblogs.com/wgwg/p/13295033.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!