3 回答

TA貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超13個(gè)贊
我會(huì)使用字符串連接并將字符串列格式化為:
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貢獻(xiàn)1719條經(jīng)驗(yàn) 獲得超6個(gè)贊
用于Series.str.extract
從 獲取整數(shù)indicator_short
,從列中的浮點(diǎn)數(shù)獲取整數(shù)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貢獻(xiàn)1865條經(jīng)驗(yàn) 獲得超7個(gè)贊
替換為using后.str.cat()
對(duì)concat
兩列使用ind
Indicator
.str.replace.
df['indicator_full']=(df.indicator_short.str.replace('ind','Indicator')).str.cat("("+df['year']+ ")", sep=(" ") )
添加回答
舉報(bào)