zoukankan      html  css  js  c++  java
  • Pandas的Series常用方法

    使用

    from pandas import Series

    1. 创建Series

    a. 常规创建

    >>> obj = Series([1,2,3], index=['A','B','C'])
    >>> obj
    A    1
    B    2
    C    3
    dtype: int64
    

     b. 根据字典创建

    >>> obj = Series({'a':1,'b':2,'c':3})
    >>> obj
    a    1
    b    2
    c    3
    dtype: int64
    

     c. Series嵌套Series

    >>> obj1 = Series([1,2,3],index=['a','b','c'])
    >>> obj2 = Series([4,5,6],index=['d','e','f'])
    >>> obj3 = Series([obj1, obj2],index=['name1', 'name2'])
    >>> obj3
    name1    a    1
    b    2
    c    3
    dtype: int64
    name2    d    4
    e    5
    f    6
    dtype: int64
    dtype: object
    

     2. Series追加

    >>> obj1 = Series([1,2,3],index=['a','b','c'])
    >>> obj1
    a    1
    b    2
    c    3
    dtype: int64
    >>> obj1.append(Series([4,5],index=['d','e']))
    a    1
    b    2
    c    3
    d    4
    e    5
    dtype: int64
    

    如果是嵌套的Series的追加,错误写法:obj['name1'].append(Series([1], index = ['a']));正确写法:obj.append(Series([Series([1], index = ['a'])], index = ['name1']))

    3. Series删除

    >>> obj1 = Series([1,2,3],index=['a','b','c'])
    >>> obj1
    a    1
    b    2
    c    3
    dtype: int64
    >>> obj1.drop('b')
    a    1
    c    3
    dtype: int64
    

     4. Series改

    >>> obj1 = Series([1,2,3],index=['a','b','c'])
    >>> obj1
    a    1
    b    2
    c    3
    dtype: int64
    >>> obj1.a = -1
    >>> obj1['b'] = -2
    >>> obj1
    a   -1
    b   -2
    c    3
    dtype: int64
    

     5. Series查

    >>> obj1 = Series([1,2,3],index=['a','b','c'])
    >>> obj1
    a    1
    b    2
    c    3
    dtype: int64
    >>> print(obj1.a == 1)
    True
    
  • 相关阅读:
    C macro : the " do { ... } while(0)" magic
    sscanf()函数
    poj-1200-hash-
    hduoj-1735 简单的贪心算法
    hduoj -2570-简单的贪心算法入门
    分治算法应用-最近点对的最小距离-hdu 1007 Quoit Design
    分治算法(转载)
    快速幂总结
    poj 1065 贪心算法
    toj ~3988~递归二叉树三种遍历的转换
  • 原文地址:https://www.cnblogs.com/niulang/p/13984352.html
Copyright © 2011-2022 走看看