Python 获取 DataFrame 中包含数字 '1' 的列名
Python 获取 DataFrame 中包含数字 '1' 的列名
假设有一个 DataFrame df,可以使用以下代码获取所有包含 '1' 的列名:
col_names = [col for col in df.columns if '1' in col]
这将生成一个列表,其中包含所有包含 '1' 的列名。
如果您只想获取包含数字 '1' 的列名,可以使用正则表达式:
import re
col_names = [col for col in df.columns if re.search(r'\d1\d*', col)]
这将返回包含数字 '1' 的列名,例如 'col_1', 'col_10', 'col_21' 等。
原文地址: https://www.cveoy.top/t/topic/oiGu 著作权归作者所有。请勿转载和采集!