美文网首页
pandas series

pandas series

作者: 小吉头 | 来源:发表于2020-12-30 11:35 被阅读0次

    series是一维数组

    series创建

    #方式1,通过list创建
    t = pd.Series([1,2,3,'a','b','c'])
    print(t) 
    >>>
    0    1
    1    2
    2    3
    3    a
    4    b
    5    c
    dtype: object
    
    print(type(t))
    >>>
    <class 'pandas.core.series.Series'>
    
    #默认索引从0开始,指定索引,索引列表长度要和数据长度一致,否则会抛异常
    t = pd.Series([1,2,3,'a','b','c'],index=['a','b','c','d','e','f'])
    print(t)
    >>>
    a    1
    b    2
    c    3
    d    a
    e    b
    f    c
    dtype: object
    
    #方式2,通过字典创建,字典的key会变成Series的索引
    tmp_dict = {"name":'xiaobai','age':12,'sex':'male'}
    t = pd.Series(tmp_dict)
    print(t)
    >>>
    name    xiaobai
    age          12
    sex        male
    dtype: object
    

    修改Series类型

    t1 = pd.Series([1,2,3])
    print(t1.dtype)
    >>>int64
    t2 = t1.astype(float)
    print(t2)
    >>>
    0    1.0
    1    2.0
    2    3.0
    dtype: float64
    

    Series切片和索引

    tmp_dict = {"name":'xiaobai','age':12,'sex':'male'}
    t = pd.Series(tmp_dict)
    print(t)
    >>>
    name    xiaobai
    age          12
    sex        male
    dtype: object
    
    #根据索引获取值
    print(t["age"])
    >>>12
    
    #根据位置取值
    print(t[1])
    >>>12
    
    #位置切片,取0~2的值
    print(t[0:2])
    >>>
    name    xiaobai
    age          12
    dtype: object
    
    #取位置0和2的值
    print(t[[0,2]])
    >>>
    name    xiaobai
    sex        male
    dtype: object
    
    #根据索引取值
    print(t[["name","sex"]])
    >>>
    name    xiaobai
    sex        male
    dtype: object
    
    #取某个不存在的索引,NaN
    print(t[["name","height"]])
    >>>
    name      xiaobai
    height        NaN
    dtype: object
    
    #值过滤
    print(t[t=="xiaobai"])
    >>>
    name    xiaobai
    dtype: object
    

    Series索引和值操作

    tmp_dict = {"name":'xiaobai','age':12,'sex':'male'}
    t = pd.Series(tmp_dict)
    >>>
    name    xiaobai
    age          12
    sex        male
    dtype: object
    
    #索引操作
    t_index = t.index
    print(t_index)
    >>>Index(['name', 'age', 'sex'], dtype='object')
    
    print(type(t_index))
    >>><class 'pandas.core.indexes.base.Index'>
    
    for item in t_index:
        print(item)
    >>>
    name
    age
    sex
    
    print(list(t_index))
    >>>['name', 'age', 'sex']
    
    #值操作
    t_values = t.values
    print(t_values)
    >>>['xiaobai' 12 'male']
    
    print(type(t_values))
    >>><class 'numpy.ndarray'>
    
    for item in t_values:
        print(item)
    >>>
    xiaobai
    12
    male
    
    print(list(t_values))
    >>>['xiaobai', 12, 'male']
    

    相关文章

      网友评论

          本文标题:pandas series

          本文链接:https://www.haomeiwen.com/subject/ruiqoktx.html