当前位置: 移动技术网 > IT编程>脚本编程>Python > Pyhton科学计算工具pandas数据结构Series介绍

Pyhton科学计算工具pandas数据结构Series介绍

2018年03月10日  | 移动技术网IT编程  | 我要评论

ibm笔记本驱动,k185次列车,公务员论坛qzzn

Pyhton科学计算工具pandas数据结构Series介绍

Python Data Analysis Library 或 pandas 是基于NumPy 的一种工具,该工具是为了解决数据分析任务而创建的。Pandas 纳入了大量库和一些标准的数据模型,提供了高效地操作大型数据集所需的工具。

pandas提供了大量能使我们快速便捷地处理数据的函数和方法。你很快就会发现,它是使Python成为强大而高效的数据分析环境的重要因素之一。

1. series数据结构

1.1 series的基本属性

import numpy as np
import pandas as pd
#  Series  数据结构
#  Series 相当于带有标签的一维数组,可以保存任何数据类型(整数,字符串,浮点数,Python对象等),轴标签统称为索引


s = pd.Series(np.random.rand(5))
print(s)

print('----------')

print(s.index)             # .index查看series索引,类型为rangeindex
print(list(s.index))     
print(s.values)          # .values查看series值,类型是ndarray

# 核心:series相比于ndarray,是一个自带索引index的数组 → 一维数组 + 对应索引
# 所以当只看series的值的时候,就是一个ndarray
# series和ndarray较相似,索引切片功能差别不大
# series和dict相比,series更像一个有顺序的字典(dict本身不存在顺序),其索引原理与字典相似(一个用key,一个用index)
0    0.396242
1    0.129111
2    0.972807
3    0.880938
4    0.185592
dtype: float64
----------
RangeIndex(start=0, stop=5, step=1)
[0, 1, 2, 3, 4]
[ 0.39624189  0.12911103  0.97280693  0.88093782  0.18559196]
# Series 名称属性:name


s1 = pd.Series(np.random.randn(5))
print(s1)

print('------------')

s2 = pd.Series(np.random.randn(5),name = 'practice')
print(s2)
print(s1.name, s2.name,type(s2.name))
# name为Series的一个参数,创建一个数组的 名称
# .name方法:输出数组的名称,输出格式为str,如果没用定义输出名称,输出为None

print('------------')

s3 = s2.rename('ggg')
print(s3)
print(s3.name, s2.name)

# .rename()重命名一个数组的名称,并且新指向一个数组,原数组不变
0    2.812936
1    1.073512
2    0.770453
3    0.382940
4   -0.620083
dtype: float64
------------
0    0.580068
1   -0.318154
2    0.506983
3    0.418464
4   -1.849126
Name: practice, dtype: float64
None practice 
------------
0    0.580068
1   -0.318154
2    0.506983
3    0.418464
4   -1.849126
Name: ggg, dtype: float64
ggg practice

1.2 series的创建

# Series 创建方法一:由字典创建,字典的key就是index,values就是values

dic = {'a':1, 'b':2, 'c':3, 'd':4}
s = pd.Series(dic)
print(s)

print('----------------')

# Series 创建方法二:由数组创建(一维数组)

ar = np.random.rand(5)
s = pd.Series(ar,index = ['a','b','c','d','e'],dtype = np.object)
print(s)

#  index参数:设置index,长度保持一致
#  dtype参数:设置数值类型
a    1
b    2
c    3
d    4
dtype: int64
----------------
a    0.0702124
b     0.660573
c     0.204302
d     0.305734
e    0.0667643
dtype: object
#  Series 创建方法三:由标量创建

s = pd.Series(10,index = range(4))
print(s)

# 如果data是标量值,则必须提供索引。该值会重复,来匹配索引的长度
0    10
1    10
2    10
3    10
dtype: int64

2.Series 的索引

Series 的索引有多重方法:
位置下标/标签索引/切片索引/布尔型索引

2.1 位置下标索引

#位置下标索引,类似序列形式的索引

s= pd.Series(np.random.rand(5))
print(s)
print(s[0],type(s[0]))      

print('----------------')

print(float(s[1]),type(float(s[1])))
# print(s[-1])       #index为数字的时候,下标必须与index相同,但如果是字符的,则可以这样表示


# 位置下标从0开始
# 输出结果为numpy.float格式,
# 可以通过float()函数转换为python float格式
# numpy.float与float占用字节不同
0    0.800861
1    0.044771
2    0.314755
3    0.118102
4    0.143970
dtype: float64
0.800860827506 
----------------
0.044770718909993956 

2.2 标签索引

#标签索引

s = pd.Series(np.random.rand(5),index = (i for i in 'abcde'))
print(s)
print(s[-2])      #  因为index为字符,所以位置索引可以使用负数来表示从后至前索引
print(s['a'])      #  因为index为字符,所以索引也要用字符

print('------------')

print(s[['a','b','c']])    #当需要所以多个标签时,要再加个中括号[[]] 来表示
print(s[['b','a','c']])   #索引的结果是一个新的series。
a    0.049365
b    0.390323
c    0.378763
d    0.445687
e    0.161719
dtype: float64
0.44568734186
0.0493651688956
------------
a    0.049365
b    0.390323
c    0.378763
dtype: float64
b    0.390323
a    0.049365
c    0.378763
dtype: float64

2.3 切片

# 切片


s1 = pd.Series(np.random.randint(0,5,5))
s2 = pd.Series(np.random.randint(0,5,5), index = ['a','b','c','d','e'])

print(s1)
print(s2)

print('------------')

print(s1[1:4],s1[4])    #当index为数字时,默认下标    左闭右开
print(s2['a':'c'])       #标签的   左闭右闭 即用index做切片 ,末端包含

print('------------')

print(s2[:-1])
print(s2[::2])
# 下标索引做切片,和list写法一样
0    2
1    3
2    4
3    1
4    2
dtype: int32
a    1
b    1
c    3
d    3
e    4
dtype: int32
------------
1    3
2    4
3    1
dtype: int32 2
a    1
b    1
c    3
dtype: int32
------------
a    1
b    1
c    3
d    3
dtype: int32
a    1
c    3
e    4
dtype: int32

2.4 布尔索引

# 布尔索引


s = pd.Series(np.random.rand(3)*100)
s[4] = None  # 添加一个空值
print(s)

print('---------')

bs1 = s > 50
bs2 = s.isnull()
bs3 = s.notnull()
print(bs1, type(bs1), bs1.dtype)
print(bs2, type(bs2), bs2.dtype)
print(bs3, type(bs3), bs3.dtype)
0    57.9164
1    30.0486
2    19.5511
4       None
dtype: object
0     True
1    False
2    False
4    False
dtype: bool  bool
0    False
1    False
2    False
4     True
dtype: bool  bool
0     True
1     True
2     True
4    False
dtype: bool  bool
-----

3.Series的基本操作

3.1 数据查看

#  数据查看


s = pd.Series(np.random.rand(50))
print(s.head())     #查看头部数据,默认前5条。
print('--------------')
print(s.tail())       # 查看尾部数据,默认后5条
0    0.430977
1    0.641213
2    0.179040
3    0.512488
4    0.310470
dtype: float64
45    0.968748
46    0.997701
47    0.460488
48    0.732198
49    0.942010
dtype: float64

3.2 重新索引

# 重新索引
''' .reindex 将会根据索引重新排列,如果当前索引不存在,则引入缺失值。其并不是改变
    索引名字的方法。'''

s = pd.Series(np.random.rand(3),index = ['a','b','c'])
print(s)
print('----------')

s1 = s.reindex(i for i in 'abdf')
s2 = s.reindex(['c','e','a'],fill_value = 2)   # fill_value参数:填充缺失值的值
print(s1)
print(s2)
a    0.125701
b    0.141712
c    0.236020
dtype: float64
----------
a    0.125701
b    0.141712
d         NaN
f         NaN
dtype: float64
c    0.236020
e    2.000000
a    0.125701
dtype: float64

3.3 Series 对齐

# Series对齐


s1 = pd.Series(np.random.rand(3), index = ['Jack','Marry','Tom'])
s2 = pd.Series(np.random.rand(3), index = ['Wang','Jack','Marry'])
print(s1)
print(s2)

print('----------')

print(s1 + s2)

# Series 和 ndarray 之间的主要区别是,Series 上的操作会根据标签自动对齐
# index顺序不会影响数值计算,以标签来计算
# 空值和任何值计算结果扔为空值
Jack     0.053361
Marry    0.514777
Tom      0.619459
dtype: float64
Wang     0.700763
Jack     0.444763
Marry    0.277073
dtype: float64
----------
Jack     0.498124
Marry    0.791850
Tom           NaN
Wang          NaN
dtype: float64

3.4 Series 删除

# 删除:.drop

s = pd.Series(np.random.rand(4),index = list('abcd'))
print(s)
print(s.drop('d'))
print(s.drop(['b','a']))    #同索引的方法,多个标签要使用[[]] 。

# 可以看出 .drop 删除元素后返回的是一个副本,原数据并没有改动 
a    0.549108
b    0.493618
c    0.608829
d    0.584887
dtype: float64
a    0.549108
b    0.493618
c    0.608829
dtype: float64
c    0.608829
d    0.584887
dtype: float64

3.5 Series 添加

# 添加


s1 = pd.Series(np.random.rand(5))
s2 = pd.Series(np.random.rand(5), index = list('abcde'))
print(s1)
print(s2)
s1[5] = 100
s2['j'] = 100
print(s1)
print(s2)
print('-----')
# 直接通过下标索引/标签index添加值

s3 = s1.append(s2)
print(s3)
# 通过.append方法,直接添加一个series
# .append方法生成一个新的Series,不改变之前的series
0    0.414305
1    0.911936
2    0.961386
3    0.152618
4    0.262118
dtype: float64
a    0.547247
b    0.205410
c    0.826322
d    0.725367
e    0.674161
dtype: float64
0      0.414305
1      0.911936
2      0.961386
3      0.152618
4      0.262118
5    100.000000
dtype: float64
a      0.547247
b      0.205410
c      0.826322
d      0.725367
e      0.674161
j    100.000000
dtype: float64
-----
0      0.414305
1      0.911936
2      0.961386
3      0.152618
4      0.262118
5    100.000000
a      0.547247
b      0.205410
c      0.826322
d      0.725367
e      0.674161
j    100.000000
dtype: float64

3.6 Series修改

# 修改

s = pd.Series(np.random.rand(3), index = ['a','b','c'])
print(s)
s['a'] = 100
s[['b','c']] = 200
print(s)
# 通过索引直接修改,类似序列
a    0.256797
b    0.531642
c    0.133652
dtype: float64
a    100.0
b    200.0
c    200.0
dtype: float64

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网