Pandas (1) - Series

2748 ワード

Series

  • pandas
    -シリーズ:1 Dアレイ、Vector
    -DataFrame:Matrix
  • 2 Dアレイ
  • Series
    -シリーズ:1次元配列、Vector(すべてのデータ型が同じ)
    -dtypeを指定しない場合、最大容量のdtype(stringはobject)を指定します.
    - index, values, dtype
    -indexは0から始まりますが、明示的に指定することもできます.
    Ex.宣言
    ```
    import numpy as np
    import pandas as pd
    from pandas import Series, DataFrame
    # -------------------------------------------
    ser = Series([1, 2, 3, 4, 5])
    ser
    #
    0    1
    1    2
    2    3
    3    4
    4    5
    dtype : int64
    ```    
    Ex.dtypeの確認
    ```
    import numpy as np
    import pandas as pd
    from pandas import Series, DataFrame
    # -------------------------------------------
    ser = Series([1, '2', 3.0, '네번째', 5])
    ser[0]     # 1
    ser[3]     # 네번째
    ser.dtype  # object ; 가장 용량이 큰 type으로 설정
    ```        
    Ex. index, values, dtype
    ```
    ser1 = Series(np.random.randint(10, 20, 5))
    ser1.index     # RangeIndex(start=0, stop=5, step=1)
    ser1.values    # [12 12 15 15 16]
    ser1.dtype     # int32
    ```            
    Ex.indexを明示的に指定できます
    ```
    ser1 = Series(np.random.randint(10, 20, 5), index = list('abcde'))
    ser1.index     # Index(['a', 'b', 'c', 'd', 'e'], dtype='object')
    ser1.values    # [19 19 14 17 18]
    ser1.dtype     # int32
    ser1.shape     # (5,)
    ```                
  • クエリー
  • シリーズ値
    - indexing/slicing
    -数値の使用:最後の数値インデックスは含まれません
    -ラベルの使用:最後のラベルを含める
    Ex.
    ```
    ser1 = Series(np.random.randint(10, 20, 5), index = list('abcde'))  # [19 19 14 17 18]
    ser1[0]			# 19
    ser1['a'] 		# 19
    ser1[1:4]	    # 19 14 17
    ser1['b':'d']   # 19 14 17
    ```         
  • シリーズ間の演算
    -AN:データがありません.float64
    Ex.
    ```
    ser1 = Series(np.random.randint(10, 20, 5), index = list('abcde'))  # [19 19 14 17 18]
    ser1_1 = ser1[::2]  # step 2
    # ------- ser1 -------
    a    19
    b    19
    c    14
    d    17
    e    18
    dtype : int32
    # ------- ser1_1 -------
    a    19
    c    14
    e    18
    dtype : int32
    # ------- 연산 -------
    resSer = ser1 + ser1_1
    resSer
    a    38.0
    b    NaN
    c    28.0
    d    NaN
    e    36.0
    dtype : float64
    ```            
  • クエリ
  • 欠落データ{{くえり:
  • }}
    -isnull()ifna():NaN値の場合はTrue
    -notnull():NaN値でない場合はTrue
    Ex.
    ```
    # ------- isnull -------
    resSer.isnull()
    a    False
    b    True
    c    False
    d    True
    e    False
    dtype : bool
    # ------- notnull -------
    resSer.notnull()
    a    True
    b    False
    c    True
    d    False
    e    True
    dtype : bool
    # ------------------------
    resSer.isnull().sum()  # 2
    ```