![週末Pythonゆる講座の登場人物(ぶち先生とゆうちゃん)](https://smart-hint.com/wp-content/uploads/2022/02/header.png)
目次
いきなり実践から始めよ
![](https://smart-hint.com/wp-content/uploads/2022/02/男性_2.png)
さて、前回はPython(Google Colaboratory)のノートブックの使い方についてご紹介しました
今回はExcelのデータを読み取って、Pythonで操作する方法をご説明していきます
![](https://smart-hint.com/wp-content/uploads/2022/02/女性_1.png)
さっそく実践ができるんですね!
![](https://smart-hint.com/wp-content/uploads/2022/02/男性_3.png)
Pythonに関する本を見ると、基礎から説明しているものを多く見かけるけど
初心者は利用イメージが湧かないため挫折してしまう・・・
![](https://smart-hint.com/wp-content/uploads/2022/02/女性_4.png)
実は本屋で立ち読みしたんですが、難しくてそっと閉じました・・・
![](https://smart-hint.com/wp-content/uploads/2022/02/男性_5.png)
Pythonが分かる人向けに「分かりにくく」書かれている本がほんとに多い・・・
![](https://smart-hint.com/wp-content/uploads/2022/02/女性_3.png)
独学でやろうとしたらすぐ挫折していました・・・
![](https://smart-hint.com/wp-content/uploads/2022/02/男性_4.png)
なので実際のデータで実践していくことで、Pythonを使うイメージを持ってもらいます
![](https://smart-hint.com/wp-content/uploads/2022/02/男性_2.png)
実践しているうちに、やりたいことができない場面が訪れるはずなので
その時に基礎を学べばよいと思っています!
![](https://smart-hint.com/wp-content/uploads/2022/02/女性_1.png)
スポーツでも、まずは試合に出て楽しさを実感してから、基礎練習をやるのに似ていますね!
![](https://smart-hint.com/wp-content/uploads/2022/02/男性_2.png)
その通り!だから実際のデータを使ってPythonを学んでいきましょう
Excelを読み取る
![](https://smart-hint.com/wp-content/uploads/2022/02/男性_2.png)
さっそくExcelのデータを読み取ってみましょう!
まずはサンプルのデータをダウンロードしてください!
![](https://smart-hint.com/wp-content/uploads/2022/02/女性_2-1.png)
ダウンロードできました!
![](https://smart-hint.com/wp-content/uploads/2022/02/男性_2.png)
もちろんお手元のExcelデータでも大丈夫ですよ!
![](https://smart-hint.com/wp-content/uploads/2022/02/女性_1.png)
持っているExcelデータもあとで試してみます!
![](https://smart-hint.com/wp-content/uploads/2022/02/男性_1.png)
まずはExcelをGoogle Driveのどこかのフォルダに入れて、Google Colaboratoryとの連携をします
そこからは記事の通りに進めてください
![](https://smart-hint.com/wp-content/uploads/2021/03/Google-Drive_2-520x300.png)
![](https://smart-hint.com/wp-content/uploads/2022/02/女性_4.png)
連携までできました!そこからどうすればよいですか??
![](https://smart-hint.com/wp-content/uploads/2022/02/男性_1.png)
ExcelをPythonで読み取るにはread_excelを使います
![](https://smart-hint.com/wp-content/uploads/2022/02/男性_2.png)
Google Driveのパスを()括弧の中に記載しましょう
※パスの場所が違うと思うので***の部分を変えてください!
pd.read_excel('/content/drive/MyDrive/***/student.xlsx')
![](https://smart-hint.com/wp-content/uploads/2022/02/女性_4.png)
あれ!?コードをコピーして実行しても、エラーが出てしまいました!?
![PythonのPnadasをインポートしていないエラー](https://smart-hint.com/wp-content/uploads/2022/02/image-41.png)
![](https://smart-hint.com/wp-content/uploads/2022/02/男性_5.png)
あ、しまった。Pandasをインポートするの忘れていました
![](https://smart-hint.com/wp-content/uploads/2022/02/女性_4.png)
パンダス?パンダのことですか??
![](https://smart-hint.com/wp-content/uploads/2022/02/男性_1.png)
read_excelの機能が入っているライブラリをPandasと呼びます
Pythonでは1から関数や仕組みを作る必要はなく、あらかじめ用意されている機能を使うことができます!
![](https://smart-hint.com/wp-content/uploads/2022/02/女性_4.png)
ライブラリ?いまいちまだ理解できていません・・・
![](https://smart-hint.com/wp-content/uploads/2022/02/男性_2.png)
例えば「スマホ」を使うときに、「アプリ」をダウンロードしますよね?
それに似ていて、やりたいことに応じて「ライブラリ」をインポートします!
![](https://smart-hint.com/wp-content/uploads/2022/02/女性_1.png)
なるほど、それはイメージできます!
![](https://smart-hint.com/wp-content/uploads/2022/02/男性_1.png)
「スマホ」にデフォルトの機能があるように、「Python」だけでも単純な計算やデータを扱うことができます
一方でライブラリをインポートすることで、より高度な分析やキレイなグラフを作ることが可能です!
![](https://smart-hint.com/wp-content/uploads/2022/02/女性_4.png)
今回はPandasというライブラリ(アプリ?)をインポートするんですね!
そもそもPandasは何ができるんですか?
![](https://smart-hint.com/wp-content/uploads/2022/02/男性_2.png)
Pandasはデータ分析ができるライブラリです!
利用するためには下記コードを書くだけでOK
import pandas as pd
![](https://smart-hint.com/wp-content/uploads/2022/02/女性_4.png)
細かいところなんですが、pdって何ですか?
![](https://smart-hint.com/wp-content/uploads/2022/02/男性_1.png)
Pandasの略がpdで慣例としてこのように使うことが多いです
頭に付けて機能を使っていきます!
![](https://smart-hint.com/wp-content/uploads/2022/02/女性_1.png)
だからpd.read_excelとなっていたんですね!
これで改めて実行します!
![Pythonのデータイメージ](https://smart-hint.com/wp-content/uploads/2022/02/image-42.png)
![](https://smart-hint.com/wp-content/uploads/2022/02/女性_5.png)
うまくデータが出ましたー!!!!
![](https://smart-hint.com/wp-content/uploads/2022/02/男性_4.png)
簡単にExcelのデータをPythonで読み取ることができましたね!
![](https://smart-hint.com/wp-content/uploads/2022/02/女性_2-1.png)
思ったより簡単でした!もっと複雑にコードを書くのかと思いました・・・
![](https://smart-hint.com/wp-content/uploads/2022/02/男性_2.png)
Jupyter Notebookを使っている方で、Pythonが同じパスに入っているときはファイル名だけでも大丈夫です!
pd.read_excel('student.xlsx')
変数を使いこなす!
![](https://smart-hint.com/wp-content/uploads/2022/02/女性_1.png)
よーし!さっそくデータ分析していくぞー!
![](https://smart-hint.com/wp-content/uploads/2022/02/男性_2.png)
いいね!データの表を見て、分析のイメージができてきたのかな
ただその前に非常に重要な「変数」を覚えて欲しい!
![](https://smart-hint.com/wp-content/uploads/2022/02/女性_4.png)
へんすう?? おかしな数字ってことですか?
![](https://smart-hint.com/wp-content/uploads/2022/02/男性_3.png)
全然違う・・・
「変数」とは簡単に言うと「名前が付いている箱」です!
![](https://smart-hint.com/wp-content/uploads/2022/02/女性_4.png)
箱?プログラミングで箱を使うんですか??
![](https://smart-hint.com/wp-content/uploads/2022/02/男性_2.png)
そう、箱に入れておくことで「追加」や「変更」が自由にできるんだ
また繰り返し使うときも楽に書ける!
![](https://smart-hint.com/wp-content/uploads/2022/02/女性_4.png)
うーん、分かるような、分からないような・・・
![](https://smart-hint.com/wp-content/uploads/2022/02/男性_1.png)
ルールさえ守れば非常に便利な機能だから覚えて欲しい
- 変数名は自由につけてOK(Pythonのデフォルト機能と被っているものはNG)
- 変数名(名前の付いた箱)を左に、=イコールを挟んで、入れたい内容を右に書く
![](https://smart-hint.com/wp-content/uploads/2022/02/男性_2.png)
=イコールを書くので「同じ」という意味に思ってしまいますが、変数という箱に代入するイメージです
![](https://smart-hint.com/wp-content/uploads/2022/02/女性_1.png)
変数の中に入れる!ですね!
![](https://smart-hint.com/wp-content/uploads/2022/02/男性_1.png)
「a」と「b」という名前の付いた箱(変数)に、数字を入れてみましょう
![](https://smart-hint.com/wp-content/uploads/2022/02/女性_2-1.png)
では「a」には10を、「b」には20を入れてみました!
これで合ってますか?
a = 10
b = 20
![](https://smart-hint.com/wp-content/uploads/2022/02/男性_4.png)
合ってます!それでは「a」+「b」をやってみましょう
![a+bのイメージ](https://smart-hint.com/wp-content/uploads/2022/02/image-43.png)
![](https://smart-hint.com/wp-content/uploads/2022/02/女性_4.png)
お、あれ。30が出てきた・・・
![](https://smart-hint.com/wp-content/uploads/2022/02/男性_1.png)
「a」の箱(変数)には10が入っていて、「b」には20が入っていました
足せば30になりますよね!
![](https://smart-hint.com/wp-content/uploads/2022/02/女性_4.png)
まあ、当たり前な気もしますが・・・
![](https://smart-hint.com/wp-content/uploads/2022/02/男性_2.png)
ちなみに下記のようなコードを書くと、自分自身の変数も更新することができます
a = a + b
![](https://smart-hint.com/wp-content/uploads/2022/02/女性_2-1.png)
これだと「a」が10から30になるんですかね!?
![](https://smart-hint.com/wp-content/uploads/2022/02/男性_2.png)
そのとおり!変数のイメージはできてきましたね!
Excelデータも変数に入れる!?
![](https://smart-hint.com/wp-content/uploads/2022/02/男性_2.png)
実は先ほどのExcelで読み取ったデータも変数に入れることができます!
![](https://smart-hint.com/wp-content/uploads/2022/02/女性_4.png)
表も変数に入れられるんですか??
![](https://smart-hint.com/wp-content/uploads/2022/02/男性_1.png)
そう、データ分析は変数をどんどん作っていって進めていきます!
![](https://smart-hint.com/wp-content/uploads/2022/02/女性_4.png)
どんなふうに箱に入れるんですか?
![](https://smart-hint.com/wp-content/uploads/2022/02/男性_2.png)
全く同じですよ!
これもPythonの慣例ですが「df」という変数を使っていきます
df = pd.read_excel('/content/drive/MyDrive/***/student.xlsx')
![](https://smart-hint.com/wp-content/uploads/2022/02/女性_3.png)
「df」にデータを入れることができました
でも表がでません・・・
![](https://smart-hint.com/wp-content/uploads/2022/02/男性_2.png)
上のコードでは変数にread_excelの情報を入れているだけなので、特に結果が出力されません
変数「df」に対して、始めの5行を出力するheadを使ってみましょう
※.ピリオドを忘れずに
![Pythonのデータ読み取りイメージ](https://smart-hint.com/wp-content/uploads/2022/02/image-44.png)
![](https://smart-hint.com/wp-content/uploads/2022/02/女性_5.png)
やった、データが出ました!
![](https://smart-hint.com/wp-content/uploads/2022/02/男性_4.png)
この変数をまた別の変数に入れたりして、データの追加や集計を行っていきます
![変数を使ったデータ分析の流れ](https://smart-hint.com/wp-content/uploads/2022/02/image-45.png)
![](https://smart-hint.com/wp-content/uploads/2022/02/女性_5.png)
流れはイメージできました!
![](https://smart-hint.com/wp-content/uploads/2022/02/男性_1.png)
今回はここまでにしましょう!次からテストのデータをいじっていきますよ!
read_excelについての細かい説明は記事をご覧ください!
![](https://smart-hint.com/wp-content/uploads/2021/03/read_excel_2-520x300.png)
まとめ
- read_excelでExcelデータを読み取ることができる
- 変数は名前の付いた箱
- 読み取ったExcelのデータも変数に入れることができる
![](https://smart-hint.com/wp-content/uploads/2022/03/ゆる講座_groupby-520x300.jpg)