参考文献のpandasのdocumentと同じものを用います。
>>> import pandas as pd
>>> df = pd.DataFrame([[1, 2], [4, 5], [7, 8]],
... index=['cobra', 'viper', 'sidewinder'],
... columns=['max_speed', 'shield'])
>>> df
max_speed shield
cobra 1 2
viper 4 5
sidewinder 7 8
要素の抽出
1つの行の取り出し
loc[<index名>]で指定した<index名>行を抽出します。
>>> df
max_speed shield
cobra 1 2
viper 4 5
sidewinder 7 8
>>> df.loc["cobra"]
max_speed 1
shield 2
Name: cobra, dtype: int64
複数行の取り出し
loc[[<index名1>,<index名2>]]で<index名1>,<index名2>の行を取り出します。
>>> df
max_speed shield
cobra 1 2
viper 4 5
sidewinder 7 8
>>> df.loc[["cobra", "viper"]]
max_speed shield
cobra 1 2
viper 4 5
要素の取り出し
loc[<index名>,<column名>]で<index名>行、<column名>列の要素を取り出します。
>>> df
max_speed shield
cobra 1 2
viper 4 5
sidewinder 7 8
>>> df.loc["cobra", "max_speed"]
1
要素の変更
単一の要素の値の変更
loc[[<index名>],[<column名>]] =
>>> df.loc[["cobra"]]
max_speed shield
cobra 1 2
>>> df.loc[["cobra"], ["max_speed"]] = 100
>>> df.loc[["cobra"]]
max_speed shield
cobra 100 2
複数要素の値の変更
loc[[<index名1>], [<index名2>], [<column名>]] =
>>> df.loc[["cobra", "viper"]]
max_speed shield
cobra 1 2
viper 4 5
>>> df.loc[["cobra", "viper"], ["max_speed"]] = 100
>>> df.loc[["cobra", "viper"]]
max_speed shield
cobra 100 2
viper 100 5
参考文献
Pythonデータ分析/機械学習のための基本コーディング! pandasライブラリ活用入門 (impress top gear)
https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.loc.html

