0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

pandasのブールインデクシング

Posted at

pandasのブールインデクシング

pandasのデータフレームに対してブール値のSeriesを使ってフィルタリングする方法をブールインデクシングと呼ぶ。これは特定の条件に基づいてデータフレームの行を選択する。

基本的な例

まず、サンプルデータフレームを作成する:

import pandas as pd

data = {
    'gene_name': ['gene1', 'gene2', 'gene3', 'gene4'],
    'log2FoldChange': [2.5, -1.2, 0.5, 3.0],
    'p-value': [0.01, 0.2, 0.05, 0.001]
}
df = pd.DataFrame(data)

次に、p-value 列の値が0.05未満である行を選択するためのブールインデクシングを行う:

selected_pval = 0.05
condition = df['p-value'] < selected_pval
df_filtered = df[condition]

condition は以下のようなブール値のSeriesになる:

0     True
1    False
2    False
3     True

df[condition] という構文により、condition が True の行のみが選択され、新しいデータフレーム df_filtered が得られる:

  gene_name  log2FoldChange   p-value
0     gene1             2.5  0.010
3     gene4             3.0  0.001
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?