我的数据框具有以下列:
'TERRITORY', 'FIELD OF STUDY', 'SELECT YEAR T (ACADEMIC YEAR = T-1 OR T)', 'VALUE'
如果数据框列名称包含"YEAR"
,我想替换"SELECT YEAR"
。该怎么做?
答案 0 :(得分:1)
一种简单的解决方案是使用列表理解:
df.columns = ["YEAR" if "SELECT YEAR" in x else x for x in df.columns]
熊猫解决方案:
df.columns = df.columns.where(~df.columns.str.contains('SELECT YEAR'), 'YEAR')
仅在设置多个值时小心,然后选择时会出现问题,因为:
print (df['YEAR'])
返回所有列YEAR
。
示例:
c = ['SELECT YEAR d', 'FIELD OF STUDY', 'SELECT YEAR T (ACADEMIC YEAR = T-1 OR T)', 'VALUE']
df = pd.DataFrame(0, columns=c, index=[1,2])
df.columns = ["YEAR" if "SELECT YEAR" in x else x for x in df.columns]
print (df)
YEAR FIELD OF STUDY YEAR VALUE
1 0 0 0 0
2 0 0 0 0
print (df['YEAR'])
YEAR YEAR
1 0 0
2 0 0