3 回答
TA贡献1829条经验 获得超13个赞
我会使用字符串连接并将字符串列格式化为:
years = '('+df['year'].astype(str).str.replace(r'.0$','')+')'
# years = '('+df['year']+')' if the year col is a string
df['indicator_full '] = ('Indicator_'+df.indicator_short.str.rsplit('_').str[-1]) \
.str.cat(years, sep=' ')
print(df)
year indicator_short indicator_full
0 2020.0 ind_1 Indicator_1 (2020)
1 2019.0 ind_2 Indicator_2 (2019)
2 2019.0 ind_3 Indicator_3 (2019)
3 NaN ind_4 Indicator_4 (nan)
TA贡献1719条经验 获得超6个赞
用于Series.str.extract
从 获取整数indicator_short
,从列中的浮点数获取整数year
并最后连接在一起:
i = df['indicator_short'].str.extract('(\d+)', expand=False)
y = df['year'].astype('Int64').astype(str).replace('<NA>','N/A')
df['indicator_full'] = 'Indicator_' + i + ' (' + y + ')'
print (df)
0 2020.0 ind_1 Indicator_1 (2020)
1 2019.0 ind_2 Indicator_2 (2019)
2 2019.0 ind_3 Indicator_3 (2019)
3 NaN ind_4 Indicator_4 (N/A)
TA贡献1865条经验 获得超7个赞
替换为using后.str.cat()
对concat
两列使用ind
Indicator
.str.replace.
df['indicator_full']=(df.indicator_short.str.replace('ind','Indicator')).str.cat("("+df['year']+ ")", sep=(" ") )
添加回答
举报