【解説】pandasとJupyter Notebookでやってみる!はじめてのデータ分析

「エクセルで売上グラフを作るのは慣れてるけど、Pythonでやると何が違うの?」
そんな疑問を持った方に向けて、今回は Jupyter Notebookを使った簡単なデータ分析 の例をご紹介します。

Python × Excelでできること
pandas

Pythonでは「pandas(パンダス)」というライブラリを使って、Excelのデータを読み込んで分析できます。
今回は「顧客ごとの月別売上データ」をグラフで可視化してみましょう。

コード実行結果

先に実行結果から。Jupyter Notebookで実行すると、
顧客ごとに月ごとの売上推移を示すカラフルな折れ線グラフが表示されます。

「どの顧客の売上が増えているのか」
「季節による変動があるのか」
といった 気づき を簡単に得ることができます。

サンプルコード
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib import font_manager, rcParams

rcParams['font.family'] = 'MS Gothic'   
rcParams['axes.unicode_minus'] = False

input_file = '251108月別売上実績.xlsx'
df = pd.read_excel(input_file)

customer_col = df.columns[0]
months = df.columns[1:]

plt.figure(figsize=(12, 6))

for idx, row in df.iterrows():
    customer = row[customer_col]
    plt.plot(months, row[1:], marker='o', label=customer)

plt.title('顧客別 月別売上推移', fontsize=14)
plt.xlabel('月', fontsize=12)
plt.ylabel('売上金額', fontsize=12)
plt.xticks(rotation=45)
plt.legend(title='顧客名', bbox_to_anchor=(1.05, 1), loc='upper left')
plt.tight_layout()
plt.show()
コードをざっくり説明

Excelファイルを読み込む
 pandasを使って、ExcelのデータをそのままPythonに取り込みます。

import pandas as pd
import matplotlib.pyplot as plt
from matplotlib import font_manager, rcParams

# 日本語フォントの設定(グラフで文字化けしないように)

rcParams['font.family'] = 'MS Gothic'
rcParams['axes.unicode_minus'] = False

# Excelファイルを読み込み

input_file = '251108月別売上実績.xlsx'
df = pd.read_excel(input_file)

顧客名ごとに折れ線グラフを描く
 matplotlibで顧客ごとの月別売上を線でつなぎ、推移を比較できるようにしています。

# 列名の設定

customer_col = df.columns[0]
months = df.columns[1:]

# グラフの描画

plt.figure(figsize=(12, 6))

for idx, row in df.iterrows():
customer = row[customer_col]
plt.plot(months, row[1:], marker='o', label=customer)

フォントやレイアウトを調整
 日本語フォントを指定したり、凡例を右側に表示したりして見やすくしています。

plt.title('顧客別 月別売上推移', fontsize=14)
plt.xlabel('月', fontsize=12)
plt.ylabel('売上金額', fontsize=12)
plt.xticks(rotation=45)
plt.legend(title='顧客名', bbox_to_anchor=(1.05, 1), loc='upper left')
plt.tight_layout()
plt.show()

まとめ

エクセル感覚で扱えるPythonのデータ分析。
慣れると、毎月の集計やグラフ作成を自動化できて、とても効率的です。

このグラフをもとに、前年比や顧客ランク別の傾向も分析できます!
一歩ずつ、データ分析の世界を楽しんでいけるといいですね。

もし環境構築やコードの書き方でつまずいたら、無理せずプロに相談しましょう。
ヤマダITオフィスでは、Pythonの導入からデータ分析の実践まで、あなたと一緒に伴走します。
「データを活かして経営に生かしたい」「自社でも分析を始めたい」という方は、ぜひお気軽にお問い合わせください。