19. 配列演算としての条件論理
numpy.where(condition, x, y)
• x if condition else y(x, yは配列である必要はない)
• python的な書き方は,
[(x if c else y) for x, y, c in zip(xarr, yarr, condition)]
• 計算が遅くなるし,1次元配列でしか通用しない
• 複雑なのも結構いけちゃう
19
result = []
for i in range(n):
if cond1[i] and cond2[i]:
result.append(0)
elif cond1[i]:
result.append(1)
elif cond2[i]:
result.append(2)
else:
result.append(3)
np.where(cond1 & cond2, 0,
np.where(cond1, 1,
np.where(cond2, 2, 3)))
result = 1 * cond1 + 2 * cond2 + 3 * (cond1 | cond2)
20. 数学的で統計学的な手法
合計,平均,標準偏差とか:軸に沿っても出来る
20
Function Description
sum 配列全て/軸に沿っての合計.長さが0の配列の合計は0
mean 配列全て/軸に沿っての平均.長さが0の配列の平均はNaN.
std, var 標準偏差と分散
min, max 最小値と最大値
argmin, argmax 最小,最大要素のインデックス
cumsum 累計的な足し算
cumprod 類型的な掛け算
34. DataFrameコンストラクタの入力値
34
Type Notes
2D ndarray 行列データ,(オプション)行・列のラベル
dict of arrays, lists, or tuples 各Sequenceが列になる.Sequenceは全部同じ長さマスト
NumPy structured/record array dict of arraysと同様の扱い
dict of Series 各値が列になる.indexは合体して行のindexになる.
dict of dicts 各辞書が列になる.キーはdict of Seriesと同様の扱い.
list of dicts or Series 各アイテムが行になる.キーor indexは列のラベルになる
list of lists or tuples 2D ndarrayと同様の扱い
Another DataFrame 基本的にそのまま
NumPy MaskedArray masked valueがNA/missingになってそれ以外は2D ndarray
35. Index Object
軸のラベルやメタデータ(軸の名前とか)を保持する
• indexは不変→データを共有するときに安全
35
Class Description
Index 最も一般的.NumPyオブジェクトとして表現
Int64Index 整数値のための特別なindex
MultiIndex 階層的なindex.タプルの配列のような感じ
DatetimeIndex ナノ秒のタイムスタンプ
PeriodIndex 期間を示すindex