サンプル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
for
してた。
読んでて面白い。
そして当時の自分の検索不足と現在の知識不足を実感。。。