フリーランス 技術調査ブログ

フリーランス/エンジニア Ruby Python Nodejs Vuejs React Dockerなどの調査技術調査の備忘録

PandasのDataFrameを使ってみる

はじめに

  • 前回、ざっくりpandasでCSVファイルを読み込みを作成したが他にどのような機能があるか理解していないので、触ってみる

px-wing.hatenablog.com

DataFrameについて

  • データを表形式(行列)にして保存することでデータを扱いやすくする
  • 2次元のデータで「値」、「インデックス」、「列」で構成
  • 要素には数値のみでなく文字列を入れることも可能
  • ラベルが付けられた軸(行と列)を持つ、2次元のサイズが可変で、表形式のデータ構造である。データフレームは2次元のデータ構造。データは行と列で表形式に整列される。Pandas DataFrameは、データ、行、列の3つの主要コンポーネントで構成されている。

DataFrameで利用できる関数(Pandasの公式ページ)

pandas.pydata.org

サンプルコード1

import pandas as pd
import numpy as np

user_height_weight = np.array(
  [
    [175,165,155,167],[85,74,65,85]
  ]
)

df = pd.DataFrame(user_height_weight)

df.columns = ['山田','佐藤','田中','森田']

df.index = ['身長','体重']

print(df)

df = df.rename(columns={'山田':'隆夫'})

print(df)
  • 実行結果
    山田   佐藤   田中   森田
身長  175  165  155  167
体重   85   74   65   85
     隆夫   佐藤   田中   森田
身長  175  165  155  167
体重   85   74   65   85

サンプルコード2

import pandas as pd
import numpy as np
import pandas as pd

df = pd.DataFrame(
  data=[
    ['77','90','69','55','34'],
    ['90','40','29','80','78']
  ], 
  columns=['UserA','UserB','UserC','UserD','UserE'],
  index=['Test1','Test2']
  )

## データフレームのサイズの確認
print(df.shape)

## データフレームのカラム情報の確認
df.info()

## データフレームの型変換
df = df.astype(int)

df.info()

print(df.describe())


print(df['UserA'])

## データフレームの行・列のラベルで指定する
print(df.loc[:,['UserA']])
print(df.loc['Test1',['UserA','UserC']])

## データフレームの行・列の番号で指定
print(df.iloc[0])
print(df.iloc[1])

print(df.iloc[:,0:3])
print(df.iloc[0,[0]])
  • 実行結果
 #   Column  Non-Null Count  Dtype 
---  ------  --------------  ----- 
 0   A       2 non-null      object
 1   B       2 non-null      object
 2   C       2 non-null      object
 3   D       2 non-null      object
 4   E       2 non-null      object
dtypes: object(5)
memory usage: 96.0+ bytes
(2, 5)
<class 'pandas.core.frame.DataFrame'>
Index: 2 entries, Math to English
Data columns (total 5 columns):
 #   Column  Non-Null Count  Dtype 
---  ------  --------------  ----- 
 0   A       2 non-null      object
 1   B       2 non-null      object
 2   C       2 non-null      object
 3   D       2 non-null      object
 4   E       2 non-null      object
dtypes: object(5)
memory usage: 96.0+ bytes
<class 'pandas.core.frame.DataFrame'>
Index: 2 entries, Math to English
Data columns (total 5 columns):
(2, 5)
(2, 5)
(2, 5)
(2, 5)
<class 'pandas.core.frame.DataFrame'>
Index: 2 entries, Test1 to Test2
Data columns (total 5 columns):
 #   Column  Non-Null Count  Dtype 
---  ------  --------------  ----- 
 0   UserA   2 non-null      object
 1   UserB   2 non-null      object
 2   UserC   2 non-null      object
 3   UserD   2 non-null      object
 4   UserE   2 non-null      object
dtypes: object(5)
memory usage: 96.0+ bytes
<class 'pandas.core.frame.DataFrame'>
Index: 2 entries, Test1 to Test2
Data columns (total 5 columns):
 #   Column  Non-Null Count  Dtype
---  ------  --------------  -----
 0   UserA   2 non-null      int64
(2, 5)
<class 'pandas.core.frame.DataFrame'>
Index: 2 entries, Test1 to Test2
Data columns (total 5 columns):
 #   Column  Non-Null Count  Dtype 
---  ------  --------------  ----- 
 0   UserA   2 non-null      object
 1   UserB   2 non-null      object
 2   UserC   2 non-null      object
 3   UserD   2 non-null      object
 4   UserE   2 non-null      object
dtypes: object(5)
memory usage: 96.0+ bytes
<class 'pandas.core.frame.DataFrame'>
Index: 2 entries, Test1 to Test2
Data columns (total 5 columns):
 #   Column  Non-Null Count  Dtype
---  ------  --------------  -----
 0   UserA   2 non-null      int64
 1   UserB   2 non-null      int64
 2   UserC   2 non-null      int64
 3   UserD   2 non-null      int64
 4   UserE   2 non-null      int64
dtypes: int64(5)
memory usage: 96.0+ bytes
           UserA      UserB      UserC     UserD      UserE
count   2.000000   2.000000   2.000000   2.00000   2.000000
mean   83.500000  65.000000  49.000000  67.50000  56.000000
std     9.192388  35.355339  28.284271  17.67767  31.112698
min    77.000000  40.000000  29.000000  55.00000  34.000000
25%    80.250000  52.500000  39.000000  61.25000  45.000000
50%    83.500000  65.000000  49.000000  67.50000  56.000000
75%    86.750000  77.500000  59.000000  73.75000  67.000000
max    90.000000  90.000000  69.000000  80.00000  78.000000
Test1    77
Test2    90
Name: UserA, dtype: int64
       UserA
Test1     77
Test2     90
UserA    77
UserC    69
Name: Test1, dtype: int64
UserA    77
UserB    90
UserC    69
UserD    55
UserE    34
Name: Test1, dtype: int64
UserA    90
UserB    40
UserC    29
UserD    80
UserE    78
Name: Test2, dtype: int64
       UserA  UserB  UserC
Test1     77     90     69
Test2     90     40     29
UserA    77
Name: Test1, dtype: int64