2 回答
FFIVE
TA贡献1797条经验 获得超6个赞
使用 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
守候你守候我
TA贡献1802条经验 获得超10个赞
没有map
/ apply
+ 的解决方案是可能的lambda
。您可以将列表直接分配给系列。列表pd.Series.apply
推导式通常更有效,因为它不是矢量化的:
df = pd.DataFrame({'s': pd.Series([1, 2, 3, np.nan])})
df['t'] = [f'this is a string {i}' for i in df['s']]
print(df)
s t
0 1.0 this is a string 1.0
1 2.0 this is a string 2.0
2 3.0 this is a string 3.0
3 NaN this is a string nan
添加回答
举报
0/150
提交
取消