[英]How to remove a character from a string and convert the rest into integer or decimal, for all values in a column in pandas?
示例:我想一次轉換列 item price 下的所有值,以便它們沒有“$”符號。
order_id item_price
0 $2.39
1 $3.39
2 $3.39
3 $2.39
4 $16.98
您可以查看 pandas 文檔。
https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.str.replace.html https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas .to_numeric.html#pandas.to_numeric
https://datatofish.com/string-to-integer-dataframe/
我沒有測試就在腦海中做了這個例子: pd.to_numeric(your_df.price.str.replace("$", ""))
嘗試這個
for i in range(0,len(df)):
df['columnname'][i] = df['columnname'][i].replace('$',' ')
df
或者更快的方式
df.columnname = [x.replace('$',' ') for x in df.columnname]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.