如何选择所有以"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)
答
您可以使用数据框startwith
的str
方法:
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)