我的df包含以下列:
df.columns
['total_rech_num_6', 'total_rech_num_7', 'total_rech_num_8','max_rech_amt_6', 'max_rech_amt_7', 'max_rech_amt_8','max_amt_6', 'max_amt_7', 'max_amt_8'].
我只想选择在列名中具有“ rech”和“ 6”的列。
我试图用'rech'选择列:
recharge_cols = [col for col in df.columns if 'rech' in col]
但是我不知道如何添加其他字符串条件来获取“ rech”和“ 6”列。需要帮助!
答案 0 :(得分:1)
将and
与另一个in
语句一起使用:
recharge_cols = [col for col in df.columns if 'rech' in col and '6' in col]
print (recharge_cols)
['total_rech_num_6', 'max_rech_amt_6']
熊猫解决方案:
recharge_cols = df.columns[df.columns.str.contains('rech') &
df.columns.str.contains('6')].tolist()
print (recharge_cols)
['total_rech_num_6', 'max_rech_amt_6']
答案 1 :(得分:0)
尝试一下:
recharge_cols = [col for col in df.columns if 'rech' in col and '6' in col]
答案 2 :(得分:0)
您可以使用以下1行表达式:
recharge_cols = [i for i in list(df) if 'rech' in i and '6' in i]