データ解析でよく聞くPandas(パンダス)とはどんなものなのでしょうか。今回はPandasで一番初めに学習するSeries(シリーズ)でできることをまとめました。基本的な内容なので、初心者の方は要チェックです。
もくじ
そもそもPandasとは?
Pandasは一言でいうと、データ分析を効率的に行うことができるPythonのライブラリです。行列データを扱いやすくしたり、集計を行ったデータを簡易的に可視化したりと様々な場面で有効な万能ライブラリです。また、Pandasはオープンソース(BSDライセンス)で公開されており、個人/商用問わず、誰でも無料で利用することができます。
できることが多いため、今回はPandasの基礎となるSeriesについて、実際にどんなことができるのか例を出して紹介していきたいと思います。
Seriesをマスターする
1. Series(1次元の値のリスト)を作成する
pd.Series()を用いて、1 次元のリスト (Series, シリーズ) を作成します。
>>> import pandas as pd >>> s = pd.Series([2,4,6,8,10]) >>> s 0 2 1 4 2 6 3 8 4 10 dtype: int64
2. index付きのSeriesを作成する(array型)
ある人の国数英理社のテストの点数をSeriesで作成してみます。 `Japanese` などのkeyをSeriesはindexと表現します。
>>> s1 = pd.Series([70, 45, 80, 60, 90], index=['Japanese', 'mathematics', 'science', \ 'society', 'English']) >>> s1 Japanese 70 mathematics 45 science 80 society 60 English 90 dtype: int64
3. index付きのSeriesを作成する(dict型)
>>> s2 = pd.Series({'Japanese': 70, 'mathematics': 45, 'science': 80, \ 'society': 60, 'English': 90}) >>> s2 English 90 Japanese 70 mathematics 45 science 80 society 60 dtype: int64
4. Seriesに値を追加する
値の追加は、もう一つSeriesを作成して結合するイメージです。append()で結合することができます。
>>> s2 = pd.Series({'Japanese': 70, 'mathematics': 45, 'science': 80, \ 'society': 60, 'English': 90}) >>> s3 = pd.Series({'art': 100}) >>> s2 = s2.append(s3) >>> s2 English 90 Japanese 70 mathematics 45 science 80 society 60 art 100 dtype: int64
5. Seriesからデータを抽出する
値の一覧だけを返したい場合は、valuesが便利です。indexを使うとSeriesのindex値を返します。
>>> s2.values array([90, 70, 45, 80, 60]) >>> s2.index Index(['English', 'Japanese', 'mathematics', 'science', 'society'], dtype='object')
値へのアクセス方法は2種類あり、番号を指定するarray型とindex名を指定するdict型があります。
>>> #array型 >>> s2[0] 90 >>> #dict型 >>> s2['science'] 80
6. 条件をつけてデータを抽出する
Series内のデータに条件を付けてアクセスすることもできます。以下は75点以上の科目を抽出した例です。
>>> s2[s2>75] English 90 science 80 dtype: int64
7. Series内に特定のindexがあるか調べる
今回のサンプルでは必要ありませんが、行数の大きいSeriesを扱う際に特定のindexの存在を確かめられるのは非常に便利です。
>>> 'society' in s2 True
8. Series内にNULL値があるか調べる
NULLがあるか調べるisnullと、NULLがないか調べるnotnullがあります。
>>> pd.isnull(s2) English False Japanese False mathematics False science False society False dtype: bool >>> pd.notnull(s2) English True Japanese True mathematics True science True society True dtype: bool
9. Series同士の足し算
Seriesの便利な特徴の一つが、Series同士で足し算ができるところです。taro君とjiro君の5科目の点数を足してみましょう。
>>> taro = pd.Series({'Japanese': 70, 'mathematics': 45, 'science': 80, \ 'society': 60, 'English': 90}) >>> jiro = pd.Series({'Japanese': 40, 'mathematics': 85, 'science': 60, \ 'society': 50, 'English': 55}) >>> taro + jiro English 145 Japanese 110 mathematics 130 science 140 society 110 dtype: int64
10. Seriesに名前をつける
Seriesには名前を付与することができます。
>>> taro.name = 'taro君のテストの点数' >>> taro English 90 Japanese 70 mathematics 45 science 80 society 60 Name: taro君のテストの点数, dtype: int64
また、indexにも付与することができます。
>>> taro.index.name = '科目名' >>> taro 科目名 English 90 Japanese 70 mathematics 45 science 80 society 60 Name: taro君のテストの点数, dtype: int64
まとめ
Seriesの基本的な使い方を解説してきました。最後にこの記事のまとめを書いておきます。
- pd.Series()でSeriesを作成できる
- indexでSeriesにindexを付与できる
- append()で値を追加できる
- valuesでデータ抽出できる
- isnull, notnullでNULL値を確認
- Series同士は足せる
- nameで名前をつける