pandas-DataFrame(デーブル型のデータ)-
- 作者: Bill Lubanovic,斎藤康毅,長尾高弘
- 出版社/メーカー: オライリージャパン
- 発売日: 2015/12/01
- メディア: 単行本(ソフトカバー)
- この商品を含むブログ (3件) を見る
【前提条件】
OS:Windows 10 64-bit, version 1607
Anaconda 4.4.0(Python 3.6 version 64-bit)インストール
NumPyのインストール
【手順概要】
1.コマンドプロンプトでIPython Notebookを起動します。
2.DataFrame(デーブル型のデータ)
【手順】
1.コマンドプロンプトでIPython Notebookを起動します。
ipython notebook
2.DataFrame(デーブル型のデータ)
2.1.numpy、panadasとpandasからSeriesとDataFrameをインポートします。
import numpy as np import pandas as pd from pandas import Series,DataFrame
2.2.クリップボードからデータを読み込みます。
nfl_frame=pd.read_clipboard() nfl_frame
Out:
Rank Team Won Lost Tied Pct. First NFL Season Total Games Division 0 1 Dallas Cowboys 493 367 6 .573 1960 866 NFC East 1 2 Green Bay Packers 730 553 37 .567 1921 1,320 NFC North 2 3 Chicago Bears 744 568 42 .565 1920 1,354 NFC North 3 4 Miami Dolphins 439 341 4 .563 1966 784 AFC East 4 5 New England Patriots 476 383 9 .554 1960 868 AFC East 5 6 New York Giants 684 572 33 .543 1925 1,289 NFC East 6 7 Denver Broncos 465 393 10 .541 1960 868 AFC West 7 8 Minnesota Vikings 457 387 10 .541 1961 854 NFC North 8 9 Baltimore Ravens 181 154 1 .540 1996 336 AFC North 9 10 San Francisco 49ers 522 450 14 .537 1950 986 NFC West
NFL win–loss records
NFL win–loss records - Wikipedia
2.3.columnsでindexを、またindexを指定することで列を出力出来ます。
nfl_frame.columns
Out:
Index(['Rank', 'Team', 'Won', 'Lost', 'Tied', 'Pct.', 'First NFL Season', 'Total Games', 'Division'], dtype='object')
nfl_frame['First NFL Season']
Out:
0 1960 1 1921 2 1920 3 1966 4 1960 5 1925 6 1960 7 1961 8 1996 9 1950 Name: First NFL Season, dtype: int64
2.4.空白文字がない場合は以下のように指定することで列を出力出来ます。
nfl_frame.Team
Out:
0 Dallas Cowboys 1 Green Bay Packers 2 Chicago Bears 3 Miami Dolphins 4 New England Patriots 5 New York Giants 6 Denver Broncos 7 Minnesota Vikings 8 Baltimore Ravens 9 San Francisco 49ers Name: Team, dtype: object
2.5.複数の列を指定することも出来ます。
nfl_frame[['Team','First NFL Season']]
Out:
Team First NFL Season 0 Dallas Cowboys 1960 1 Green Bay Packers 1921 2 Chicago Bears 1920 3 Miami Dolphins 1966 4 New England Patriots 1960 5 New York Giants 1925 6 Denver Broncos 1960 7 Minnesota Vikings 1961 8 Baltimore Ravens 1996 9 San Francisco 49ers 1950
2.6.pandasからdataframeに変換します。
DataFrame(nfl_frame,columns=['Team','First NFL Season'])
2.7.存在しない列Stadiumを記述してみます。値はNaNが返ってきます。
DataFrame(nfl_frame,columns=['Team','First NFL Season','Stadium'])
2.8.行が多い場合に先頭から取得したい行を指定できます。デフォルトhead()はhead(5)です。
nfl_frame.head()
nfl_frame.head(3)
2.9.同様に行が多い場合に末尾から取得したい行を指定できます。
nfl_frame.tail()
nfl_frame.tail(3)
2.10.指定行を取得できます。
nfl_frame.ix[3]
Out:
Rank 4 Team Miami Dolphins Won 439 Lost 341 Tied 4 Pct. 0.563 First NFL Season 1966 Total Games 784 Division AFC East Name: 3, dtype: object
Indexing and Selecting Data
Indexing and Selecting Data — pandas 0.20.3 documentation
2.11.ixは現在非推奨のためlocを使うようにしましょう。(今回は行番号を指定しているのでlocですね。)
ix、iloc、locについては、以下記事が大変参考になりました。
nfl_frame.loc[3]
2.12.NaNとなっているStadium列に値を代入します。
nfl_frame['Stadium']="Levi'S Stadium"
2.13.numpyのarangeを使って値を代入することも出来ます。
nfl_frame['Stadium']=np.arange(10)
2.14.Seriesを使用して指定したindexのみに値を代入することも出来ます。
stadiums=Series(["Levi's stadium","AT&T Stadium"],index=[4,0]) nfl_frame['Stadium']=stadiums
2.15.列の削除も出来ます。
del nfl_frame['Stadium']
2.16.ディックショナリからDataFrameに変換します。
data={'City':['SF','LA','NYC'],'Population':[837000,3880000,8400000]} city_frame=DataFrame(data)
Pythonではじめる機械学習 ―scikit-learnで学ぶ特徴量エンジニアリングと機械学習の基礎
- 作者: Andreas C. Muller,Sarah Guido,中田秀基
- 出版社/メーカー: オライリージャパン
- 発売日: 2017/05/25
- メディア: 単行本(ソフトカバー)
- この商品を含むブログを見る
参考:
udemy 実践Pythonデータサイエンス