码迷,mamicode.com
首页 > 其他好文 > 详细

pandas数据结构之series

时间:2018-11-17 15:55:58      阅读:204      评论:0      收藏:0      [点我收藏+]

标签:3.3   原则   2.3   lte   lin   cal   概览   翻译   数据结构   

 

      阅读之前假定你已经有了python内置的list和dict的基础.这里内容几乎是官方文档的翻译版本.

 
概览:
技术分享图片
?
 
一个要铭记在新的基本特点是 数据对齐
要点:索引,轴标签,生成实例时传入的数据类型
?
 

#*生成pd.Series(data,index)        data是传入的数据,index是第一列的名称(即标签)      (其他不常用的参数忽略)
#ndarray (data的类型)
>>> pd.Series(np.random.randn(5))
0    1.617186
1    0.326732
2   -0.230443
3   -0.137932
4    0.474872
dtype: float64
>>> pd.Series(np.random.randn(5),index=[‘a‘, ‘b‘, ‘c‘, ‘d‘, ‘e‘])
a    0.048464
b    1.413755
c    0.036489
d    0.533946
e    0.286384
dtype: float64
如果不指定index,标签默认从0开始
#dict
>>> d = {‘b‘ : 1, ‘a‘ : 0, ‘c‘ : 2}
>>> pd.Series(d)
b    1
a    0
c    2
dtype: int64
index的顺序跟字典key的顺序一样.
>>> pd.Series(d,index=[‘b‘, ‘c‘, ‘d‘, ‘a‘])
b    1.0
c    2.0
d    NaN
a    0.0
dtype: float64
在这里,index顺序跟传入的数据一致.虽然‘d’在字典中不存在,但为了保证数据不丢失,便创建起来,其值为空.这可以理解为数据对齐
     #scalar (标量)
>>> pd.Series(5,index=[‘a‘, ‘b‘, ‘c‘, ‘d‘, ‘e‘])
a    5
b    5
c    5
d    5
e    5
dtype: int64
>>> pd.Series(‘a‘,index=[‘b‘, ‘c‘, ‘d‘, ‘a‘])
b    a
c    a
d    a
a    a
dtype: object
一整列的数据都一样
 
 

#*操作
     #ndarray-like
切片,过滤,通过索引取值
>>> se =pd.Series(np.random.randn(5),index=[‘a‘, ‘b‘, ‘c‘, ‘d‘, ‘e‘])
>>> se[:3]  #slice
a    1.169659
b   -1.557760
c    1.199475
dtype: float64
>>> se[se >se.median()]  #filter
a    1.169659
c    1.199475
dtype: float64
>>> se[[4,3,1]]  #indexing
e   -1.113787
d    0.571881
b   -1.557760
dtype: float64
#dic-like
索引,in 判断
>>> se[‘a‘]=12 #indexing
>>> ‘e‘ in se
True
#*计算:矢量加法,数乘,函数
>>> se+se
a    24.000000
b    -3.115519
c     2.398949
d     1.143761
e    -2.227573
dtype: float64
>>> se*4
a    48.000000
b    -6.231039
c     4.797899
d     2.287523
e    -4.455147
dtype: float64
>>> np.exp(se)
a    162754.791419
b         0.210607
c         3.318373
d         1.771596
e         0.328313
dtype: float64
 

#*其他:序列的命名和重命名
>>> s=pd.Series(np.random.randn(5),name=‘something‘)
>>> s
0   -0.010572
1   -0.519850
2    0.649738
3   -0.443780
4    0.402685
Name: something, dtype: float64
>>> s2=s.rename(‘different‘)
>>> s2
0   -0.010572
1   -0.519850
2    0.649738
3   -0.443780
4    0.402685
Name: different, dtype: float64
变成两个不同的序列
 
 

源码:
import pandas as pdu
import numpy as npa
n
#basic tentet:data aligment/基本的原则:数据对齐m
#point:data types;indexing;axis labeling/alignment]/要点:数据类型,索引,轴标签和对齐

def series():
    #*generate
    #ndarray
    se=pd.Series(np.random.randn(5))
    se =pd.Series(np.random.randn(5),index=[‘a‘, ‘b‘, ‘c‘, ‘d‘, ‘e‘])
    #dict
    d = {‘b‘ : 1, ‘a‘ : 0, ‘c‘ : 2}
    se=pd.Series(d)
    se=pd.Series(d,index=[‘b‘, ‘c‘, ‘d‘, ‘a‘])
    #scalar
    se=pd.Series(5,index=[‘a‘, ‘b‘, ‘c‘, ‘d‘, ‘e‘])

    #*operate
    #ndarrat-like
    se[:3]  #slice
    se[se >se.median()]  #filter
    se[[4,3,1]]  #indexing
    #dict-like
    se[‘a‘]=12 #indexing
    ‘e‘ in se

    #compute
    se+se
    se*2
    np.exp(se)

pandas数据结构之series

标签:3.3   原则   2.3   lte   lin   cal   概览   翻译   数据结构   

原文地址:https://www.cnblogs.com/vvlj/p/9973801.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!