如何选择所有以"durations"开头的列或“形状"? [英] How to select all columns that start with "durations" or "shape"?

查看:62
本文介绍了如何选择所有以"durations"开头的列或“形状"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何选择标题名称以"durations"或"shape"开头的所有列? (而不是定义一长列名称).我需要选择这些列并将空白字段替换为0.

How to select all columns that have header names starting with "durations" or "shape"? (instead of defining a long list of column names). I need to select these columns and substitute blank fields by 0.

column_names = ['durations.blockMinutes_x',
                'durations.scheduledBlockMinutes_y']
data[column_names] = data[column_names].fillna(0)

推荐答案

您可以使用数据框startwithstr方法:

You could use str methods of dataframe startwith:

df = data[data.columns[data.columns.str.startwith('durations') | data.columns.str.startwith('so')]]
df.fillna(0)

或者您可以使用contains方法:

df = data.iloc[:, data.columns.str.contains('durations.*'|'shape.*') ]
df.fillna(0)

这篇关于如何选择所有以"durations"开头的列或“形状"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆