我有一个单列数据框:
col1
1
2
3
4
我需要创建另一个列,该列将是一个字符串,如:
Result:
col1 col2
1 Value is 1
2 Value is 2
3 Value is 3
4 Value is 4
我知道格式化的字符串,但不确定如何在数据框中实现它
将列转换为字符串并添加值:
df['col2'] = 'Value is ' + df['col1'].astype(str)
或将f-string
s用于Series.map
:
df['col2'] = df['col1'].map(lambda x: f'Value is {x}')
print (df)
col1 col2
0 1 Value is 1
1 2 Value is 2
2 3 Value is 3
3 4 Value is 4
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句