一般社団法人 全国個人事業主支援協会

COLUMN コラム

takeメソッド

numpyでのIndexingによる抽出メソッドtakeを利用可能。locやilocよりも高速(但し、locほど柔軟性なし)

 

index = pd.Index(np.random.randint(0, 1000, 10))

positions = [0, 9, 3]

index[positions]

index.take(positions)

ser = pd.Series(np.random.randn(10))

ser.iloc[positions]

ser.take(positions)

 

DataFrameの場合、一次元配列かndarrayでの指定が可能

frm = pd.DataFrame(np.random.randn(5, 3))

frm.take([0, 2], axis=1)

bool IndexについてはPandasは対応していないので注意(動作するけどサポート外という厄介な内容

arr = np.random.randn(10)

arr.take([False, False, True, True]) # 動作するけど全部取れる

arr[[0, 1]]

ser = pd.Series(np.random.randn(10))

ser.take([False, False, True, True])

ser.iloc[[0, 1]]

 

The following two tabs change content below.

WATANABE REN

千葉県在住のエンジニアです。最近はPythonやってます。

この記事をシェアする

  • Twitterでシェア
  • Facebookでシェア
  • LINEでシェア