簡體   English   中英

如何將數據框中的符號轉換為 python 中的浮點數?

[英]How do I convert a symbol within a dataframe to a float in python?

我是 python 的新手,正在研究字符串操作

我有一個數據框

df['Installs']
Out[22]: 
0           10,000+
1          500,000+
2        5,000,000+
3       50,000,000+
4          100,000+
5           50,000+

如何刪除“+”並將 df 中的字符串轉換為浮點數?

我的輸入:

df['Installs'] = df['Installs'].str.replace('+','',regex=True).astype(float)

但是我收到一個錯誤:

ValueError: could not convert string to float: '10,000'

如何編輯我的代碼,以便我得到 10,000.0 作為我的輸出等其他值而不是 10,000+

使用Series.str.replace,+來清空string

df['Installs'] = df['Installs'].str.replace('[,+]','').astype(float)
#alternative
#df['Installs'] = df['Installs'].replace('[,+]','', regex=True).astype(float)
print (df)
     Installs
0     10000.0
1    500000.0
2   5000000.0
3  50000000.0
4    100000.0
5     50000.0

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM