検証環境

  • Python 3.10.8
  • pandas 1.5.2
  • matplotlib 3.6.2

はじめに

Pandasを使用すると、データの作成、読み込み、解析を簡単に行うことができる。
また、グラフも表示できるので便利である。

インストール

% pip3 install pandas

データ作成

# test.py
import pandas as pd
import numpy

dates = pd.date_range("2018/01/20", periods=10)
nums = numpy.random.randint(0, 100, (10, 2))
columns = ["既存顧客", "新規顧客"]

df = pd.DataFrame(nums, index=dates, columns=columns)

print(df)
% python3 test.py
            既存顧客  新規顧客
2018-01-20    93    99
2018-01-21    55    56
2018-01-22    87    84
2018-01-23    72     6
2018-01-24    25     0
2018-01-25    49    63
2018-01-26    94    15
2018-01-27     5    22
2018-01-28    53    10
2018-01-29     7    61

または、以下の書き方でも同様のことができる。

import pandas as pd
import numpy

dates = pd.date_range("2018/01/20", periods=10)

df = pd.DataFrame({
        "既存顧客" : numpy.random.randint(0, 100, 10),
        "新規顧客" : numpy.random.randint(0, 100, 10)
        },
        index=dates
    )

print(df)

データ読み込み

Pandasは、csvやtsvなどからデータを読み込むことができる。
今回は、以下の test.csv ファイルを読み込む。

Date,既存顧客,新規顧客
2018-01-20,93,99
2018-01-21,55,56
2018-01-22,87,84
2018-01-23,72,6
2018-01-24,25,0
2018-01-25,49,63
2018-01-26,94,15
2018-01-27,5,22
2018-01-28,53,10
2018-01-29,7,61
# test.py
import pandas as pd

df = pd.read_csv('test.csv', index_col='Date')

print(df)
% python3 test.py
            既存顧客  新規顧客
Date                  
2018-01-20    93    99
2018-01-21    55    56
2018-01-22    87    84
2018-01-23    72     6
2018-01-24    25     0
2018-01-25    49    63
2018-01-26    94    15
2018-01-27     5    22
2018-01-28    53    10
2018-01-29     7    61

データ取得

Pandas の DataFrame オブジェクトは、値を指定して柔軟に抽出することができる。

print(df['新規顧客'])
Date
2018-01-20    99
2018-01-21    56
2018-01-22    84
2018-01-23     6
2018-01-24     0
2018-01-25    63
2018-01-26    15
2018-01-27    22
2018-01-28    10
2018-01-29    61
Name: 新規顧客, dtype: int64
print(df[2:5])
既存顧客  新規顧客
Date                  
2018-01-22    87    84
2018-01-23    72     6
2018-01-24    25     0
print(df['2018-01-22':'2018-01-24'])
既存顧客  新規顧客
Date                  
2018-01-22    87    84
2018-01-23    72     6
2018-01-24    25     0

データ操作

転置

print(df.T)
% python3 test.py
Date  2018-01-20  2018-01-21  2018-01-22  2018-01-23  2018-01-24  2018-01-25  \
既存顧客          93          55          87          72          25          49   
新規顧客          99          56          84           6           0          63   

Date  2018-01-26  2018-01-27  2018-01-28  2018-01-29  
既存顧客          94           5          53           7  
新規顧客          15          22          10          61  

ソート

print(df.sort_values(by='既存顧客'))
% python3 test.py
            既存顧客  新規顧客
Date                  
2018-01-27     5    22
2018-01-29     7    61
2018-01-24    25     0
2018-01-25    49    63
2018-01-28    53    10
2018-01-21    55    56
2018-01-23    72     6
2018-01-22    87    84
2018-01-20    93    99
2018-01-26    94    15

データプロット

プロットするには、matplotlib が必要になる。

% pip3 install matplotlib

以下は「test.csv」を読み込んでグラフを表示するコードである。

import pandas as pd
import matplotlib.pyplot as plt

df = pd.read_csv('test.csv', index_col=0, encoding='utf-8')

df.plot()
plt.show()

実行結果

また、上記のコードでは日本語が正しく表示できていない。
これを解消するには、以下のコードのようにフォントファミリーを指定すれば良い。

import pandas as pd
import matplotlib.pyplot as plt
import matplotlib

df = pd.read_csv('test.csv', index_col=0, encoding='utf-8')

font = {'family' : 'YuGothic'}
matplotlib.rc('font', **font)

df.plot()
plt.show()

他にも、バーやヒストグラムなどのグラフもプロットすることができる。

df.plot(kind='bar')

参考


0件のコメント

コメントを残す

アバタープレースホルダー

メールアドレスが公開されることはありません。 が付いている欄は必須項目です