python f'string 在 pd.Series.map 函数中不起作用

pyd*_*pyd 6 python format series pandas f-string

我有一个pd系列,

s = pd.Series([1, 2, 3, np.nan])
Run Code Online (Sandbox Code Playgroud)

当我做,

s.map('this is a string {}'.format)
[out]
0    this is a string 1.0
1    this is a string 2.0
2    this is a string 3.0
3    this is a string nan   
Run Code Online (Sandbox Code Playgroud)

如何使用格式化字符串获得相同的结果?

s.map(f'this is a string {?}') ?
Run Code Online (Sandbox Code Playgroud)

jez*_*ael 5

使用 lambda 函数{x}

print (s.map(lambda x: f'this is a string {x}'))
#alternative with different value
#print (s.map(lambda val: f'this is a string {val}'))
0    this is a string 1.0
1    this is a string 2.0
2    this is a string 3.0
3    this is a string nan
dtype: object
Run Code Online (Sandbox Code Playgroud)