zoukankan      html  css  js  c++  java
  • pandas之Series

    pandas的数据结构

    Series:一维数组,与Numpy中的一维array类似。二者与Python基本的数据结构List也很相近,其区别是:List中的元素可以是不同的数据类型,而Array和Series中则只允许存储相同的数据类型,这样可以更有效的使用内存,提高运算效率。

    DataFrame: 二维的表格型数据结构。多个Series结构就组成了DataFrame数据结构,这里需要特别注意的是DataFrame是按照列来存储的。

    Panel: 三维的数组,可以理解为DataFrame的容器。

    Series定义
    Series(data, index=index),data参数可以是整形、字符串、dict、ndarray、常量值。index是索引值,如果数据类型是ndarray,index的长度需要和data的长度一致,如果index没有指定,那么索引将会从[0,....., len(data) -1]递增。

    创造一个serise数据

    import pandas as pd
    import numpy as np
    ​
    s = pd.Series([9, 'zheng', 'beijing', 128])
    ​
    print(s)

    打印

    0          9
    1      zheng
    2    beijing
    3        128
    dtype: object

    用numpy ndarray构造一个Series生成一个随机数

    import pandas as pd
    import numpy as np
    ​
    num_abc = pd.Series(np.random.randn(5), index=list('abcde'))
    num = pd.Series(np.random.randn(5))
    ​
    print(num)
    print(num_abc)
    ​
    # 打印
    0   -0.102860
    1   -1.138242
    2    1.408063
    3   -0.893559
    4    1.378845
    dtype: float64
    a   -0.658398
    b    1.568236
    c    0.535451
    d    0.103117
    e   -1.556231
    dtype: float64

    字典(dict)类型数据
    因为字典是key-value结构存储的,key相当于字典的索引了,那么在创建Series的时候若是不指定index参数,Series就默认使用字典的key来作为所以,若是指定了则在字典中去关联索引,如下:

    import pandas as pd
    import numpy as np
    
    data = {'a':10, 'b':20, 'c':30}
    
    s = pd.Series(data)
    
    print(s)
    
    s = pd.Series(data, index=['b', 'c', 'a', 'd'])
    
    print(s)
    
    OUT:
    a    10
    b    20
    c    30
    dtype: int64
    b    20.0
    c    30.0
    a    10.0
    d     NaN
    dtype: float64

    Series加法运算:

    import pandas as pd
    import numpy as np
    
    s1 = pd.Series(np.random.randn(3), index=['a','b','c'])
    
    s2 = pd.Series(np.random.randn(3), index=['a','b','c'])
    
    s3 = pd.Series(np.random.randn(2), index=['a','b'])
    
    s4 = pd.Series(np.random.randn(2), index=['e','f'])
    
    print(s1)
    
    print(s2)
    
    # 对应索引的值相加
    print('索引值相同的登结果:
    %s
    ' % (s1 + s2))
    
    print(s1)
    
    print(s3)
    
    # 对应索引的值相加
    print('索引值部分不相同的登结果:
    %s
    ' % (s1 + s3))
    
    print(s1)
    
    print(s4)
    # 对应索引的值相加
    
    print('索引值都不相同的登结果:
    %s
    ' % (s1 + s4))
    
    OUT:
    a    0.271685
    b    0.547691
    c   -0.143253
    dtype: float64
    a   -1.603913
    b   -0.464923
    c    0.471518
    dtype: float64
    索引值相同的登结果:
    a   -1.332227
    b    0.082768
    c    0.328266
    dtype: float64
    
    a    0.271685
    b    0.547691
    c   -0.143253
    dtype: float64
    a   -0.690966
    b    1.131122
    dtype: float64
    索引值部分不相同的登结果:
    a   -0.419281
    b    1.678812
    c         NaN
    dtype: float64
    
    a    0.271685
    b    0.547691
    c   -0.143253
    dtype: float64
    e   -1.898507
    f    0.153425
    dtype: float64
    索引值都不相同的登结果:
    a   NaN
    b   NaN
    c   NaN
    e   NaN
    f   NaN
    dtype: float64
  • 相关阅读:
    sql sever读取写入Excel总结
    sql sever跨数据库复制数据的方法
    SQLServer2005重建索引前后对比【转】
    SQL自定义函数
    【C】——回调函数实现泛型算法
    【C】——压缩字符串
    【C】——指针与const限定符
    loop within fields
    分类与分步在行测数量关系中的应用
    隔年增长率
  • 原文地址:https://www.cnblogs.com/llbb/p/12444872.html
Copyright © 2011-2022 走看看