如何将字符串格式的序列转换为整数

2024-10-03 17:15:29 发布

您现在位置:Python中文网/ 问答频道 /正文

代码:

E4G3G['Pmhoprepatt'] = E4G3G['Pmhoprepatt'].apply(lambda x: 
                                            "${:,.0f}".format(int(float(x))))

错误:

ValueError: could not convert string to float: '1,153.00'

输入:

E4G3G['Pmhoprepatt']
Out[57]: 
0         110.00
1         168.00
2         240.00
3         101.00
4         360.00
5         143.00
6         125.00
7         431.00
8         683.00
9         102.00
10        103.00
11        209.00
12        285.00
13        365.00

到目前为止,它没有工作,我希望没有错误,但错误不断发生。你知道吗


Tags: tolambda代码formatconvertstring错误not
1条回答
网友
1楼 · 发布于 2024-10-03 17:15:29

试试这个:

E4G3G['Pmhoprepatt']=E4G3G['Pmhoprepatt'].str.replace(',','').astype(float)

可以使用str访问器的replace方法进行替换,然后转换为float。你知道吗

输出将如预期的那样。你知道吗

相关问题 更多 >