0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Python Pandasを使ってExcelデータの相関係数を求める

Last updated at Posted at 2022-06-18

Excelにデータ分析ツールで相関係数を求める方法がありますが、
こちらではPythonのPandasライブラリを使ってExcelデータの相関係数を求めてみます。

手順としてはPythonコード(correlation.py)を作成してそれを実行してみます。

大きな流れとしては

  • ① 実行環境はPandasライブラリを使用する
  • ② Excelファイルを開いてシートを読み込む
  • ③ corr()関数を使って相関係数を求める

■ 1. Pythonコード

  • ファイル名:correlation.py
correlation.py
import pandas as pd  # Pandasライブラリをpdとして読み込む

# Excelファイル (data.xlsx)のシート名"Sheet1"を読み込む
df = pd.read_excel('data.xlsx' ,sheet_name = 'Sheet1')

#読み込んだデータの相関係数を求める
df_corr = df.corr()

#結果を出力する
print(df_corr)

#結果をcsv形式で保存する
df_corr.to_csv('output.csv')

■ 2. Excelデータ

  • ファイル名:date.xlsx
  • シート名:Sheet1

スクリーンショット 2022-06-18 10.38.07.png

■ 3. 実行

 > python correlation.py

■ 4. 結果

スクリーンショット 2022-06-25 10.47.22.png

相関係数が出力され、気温(℃)とAmount(個数)の相関係数が0.9以上となり強い相関関係があることがわかった。つまり、気温が高くなると個数は増え、低くなると個数は減る。

以上になります。
このページが皆様の何かにお役に立てれば幸いです。


0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?