美文网首页
3. 日月光华 Python数据分析-Pandas-Serie

3. 日月光华 Python数据分析-Pandas-Serie

作者: 薛东弗斯 | 来源:发表于2023-07-05 05:42 被阅读0次
    import pandas as pd
    
    s = pd.Series([1,3,6,2])
    s
    # 0    1
    # 1    3
    # 2    6
    # 3    2
    # dtype: int64
    
    s.index
    # RangeIndex(start=0, stop=4, step=1)
    
    s.values
    # array([1, 3, 6, 2], dtype=int64)
    
    s1 = pd.Series([1,3,6,2], index=['a', 'b', 'c', 'd'])
    s1
    # a    1
    # b    3
    # c    6
    # d    2
    # dtype: int64
    
    s.index
    # Index(['a', 'b', 'c', 'd'], dtype='object')
    
    s[s<3]
    # a    1
    # d    2
    # dtype: int64
    
    s*4
    # a     4
    # b    12
    # c    24
    # d     8
    # dtype: int64
    
    import numpy as np
    np.mean(s)   # 3.0
    s.mean()      # 3.0
    s.max()        # 6
    s.index        # Index(['a', 'b', 'c', 'd'], dtype='object')
    'e' in s.index  # False
    s = pd.Series({'a': 1, 'b': 9, 'c': 4})
    s
    # a    1
    # b    9
    # c    4
    # dtype: int64
    s = pd.Series({'a': 1, 'b': 9, 'c': 4}, index=['a', 'b', 'd'])
    s
    # a    1
    # b    9
    # d    NaN
    # dtype: int64
    
    s[s.isnull()]
    #d   NaN
    #dtype: float64
    
    

    自动按照索引对齐

    s
    # a    1.0
    # b    9.0
    # d    NaN
    # dtype: float64
    
    s1
    # a    1
    # b    3
    # c    6
    # d    2
    # dtype: int64
    
    s + s1
    # a     2.0
    # b    12.0
    # c     NaN
    # d     NaN
    # dtype: float64
    
    s.notnull()
    # a     True
    # b     True
    # d    False
    # dtype: bool
    
    s
    # a    1.0
    # b    9.0
    # d    NaN
    # dtype: float64
    
    s['a']   
    # 1.0
    
    s[['a', 'b']]
    # a    1.0
    # b    9.0
    # dtype: float64
    
    s1
    # a    1
    # b    3
    # c    6
    # d    2
    # dtype: int64
    
    s1['a': 'c']
    # a    1
    # b    3
    # c    6
    # dtype: int64
    
    s['a'] = 1000
    s
    # a    1000.0
    # b       9.0
    # d       NaN
    # dtype: float64
    
    s
    # a    1000.0
    # b       9.0
    # d       NaN
    # dtype: float64
    
    s[s.isnull()]
    # d   NaN
    # dtype: float64
    
    s[~s.isnull()]
    # a    1000.0
    # b       9.0
    # dtype: float64
    
    s[s.notnull()]
    # a    1000.0
    # b       9.0
    # dtype: float64
    

    取出一列,就是一个series

    相关文章

      网友评论

          本文标题:3. 日月光华 Python数据分析-Pandas-Serie

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