科学の箱

科学・IT・登山の話題

Python

dataframeで条件を付けて要素を返す方法

投稿日:2018年3月13日 更新日:

前回の続きから。

dfは現在以下のようになっている。

W X Y Z
A 2.706850 0.628133 0.907969 0.503826
B 0.651118 -0.319318 -0.848077 0.605965
C -2.018168 0.740122 0.528813 -0.589001
D 0.188695 -0.758872 -0.933237 0.955057
E 0.190794 1.978757 2.605967 0.683509

ここで要素>0を判断してみる。


df > 0

W X Y Z
A True True True True
B True False False True
C False True True False
D True False False True
E True True True True

要素>0だけを抜き出すと以下のようになる。


df[df>0]

W X Y Z
A 2.706850 0.628133 0.907969 0.503826
B 0.651118 NaN NaN 0.605965
C NaN 0.740122 0.528813 NaN
D 0.188695 NaN NaN 0.955057
E 0.190794 1.978757 2.605967 0.683509

次に列W>0の行だけを抜き出す。


df[df['W']>0]

この結果は以下のようになる。
W X Y Z
A 2.706850 0.628133 0.907969 0.503826
B 0.651118 -0.319318 -0.848077 0.605965
D 0.188695 -0.758872 -0.933237 0.955057
E 0.190794 1.978757 2.605967 0.683509

さらに特定の列だけを取得したいならば以下のような形式で取得できる。


df[df['W']>0]['X']]

A 0.628133
B -0.319318
D -0.758872
E 1.978757
Name: X, dtype: float64

メタ情報

inarticle



メタ情報

inarticle



-Python
-

執筆者:


comment

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

関連記事

no image

kaggle Titanic Tutorial – 7

さて今回はAgeを補完してから、元の分布と比べてみる。 def fill_age(row): condition = ( (d_train_g_m[‘Sex’] == row[‘Sex’]) &amp …

no image

automated the boring – day8

https://automatetheboringstuff.com/chapter15/ さてプログラムを実行しているときに案外出てくる要件が時間計測。 例えばアルゴリズム間でパフォーマンスを比較す …

no image

graphvizによる決定木の表示

graphvizの実行ファイルをインストール https://graphviz.gitlab.io/_pages/Download/Download_windows.html pythonにgraph …

no image

matplotlibのお役立ちリンク

matplotlibのリファレンスが必要ならこちらを参照する。 https://www.labri.fr/perso/nrougier/teaching/matplotlib/ Related pos …

no image

GCP google translatorの利用

こちらが詳しい https://cloud.google.com/translate/docs/reference/libraries#client-libraries-usage-python &n …

2018年3月
月 火 水 木 金 土 日
« 2月   4月 »
 1234
567891011
12131415161718
19202122232425
262728293031  

side bar top



アーカイブ

カテゴリー