0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

DataFrame の columns から特定の文字列を含む列だけ抽出したい時の方法2つ

Posted at

サンプルDataFrame

# pip install pandas
import pandas as pd

data = {
    "apple_price": [300, 200, 100],
    "apple_color": ["red", "green", "yellow"],
    "banana_price": [300, 200, 100],
    "banana_color": ["yellow", "green", "brown"]
}

df = pd.DataFrame(data)

検索キーワード

specific_str = "apple"

filter関数でカラム検索

result = df.filter(like=specific_str).columns
print(result)  # Index(['apple_price', 'apple_color'], dtype='object')
長所
  • 処理早い
  • シンプルで読みやすい
短所
  • 部分文字列の一致のみで、大文字と小文字の区別やより複雑なパターン(正規表現)は処理できない

containsでカラム検索

result = df.columns[df.columns.str.contains(pat=specific_str)]
print(result)  # Index(['apple_price', 'apple_color'], dtype='object') 
長所
  • 大文字と小文字の区別が出来る case=False
  • 正規表現の使用も可能 regex=True
  • 上記の理由から.filter(like=...)より柔軟性がある
短所
  • ちょっと長い...

以前自分が作成したコードを読み返すと無駄に for:sunglasses:for:sunglasses:してた。
読んでて面白い。
そして当時の自分の検索不足と現在の知識不足を実感。。。

0
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?