我想洗掉“HKD $”并更改為 Float 物件
我使用下面的命令列,但它不起作用。
df['price_list_1'] = df['price_list_1'].str.replace("HKD $","")
結果:
/usr/local/lib/python3.7/dist-packages/ipykernel_launcher.py:1: FutureWarning: The default value of regex will change from True to False in a future version.
"""Entry point for launching an IPython kernel.
0 $14
1 $20
2 $55
3 $20
4 $50
...
647 $58
648 $20
649 $46
650 $70
651 $70
Name: price_list_1, Length: 652, dtype: object
uj5u.com熱心網友回復:
使用 時.str.replace(),應注意字串值是否包含正則運算式的任何特殊字符,例如$、^和*,在本例中為$.
明確地說 '$' 不適用于正則運算式,您可以使用regex=False.
df['price_list_1'] = df['price_list_1'].str.replace("HKD $","", regex=False)
有關在中使用正則運算式的資訊str.replace:請參閱https://datascientyst.com/replace-values-regex-pandas/
uj5u.com熱心網友回復:
你也可以使用str.lstrip:
out = df['price_list'].str.lstrip('$').astype(float)
輸出:
0 14.0
1 20.0
2 55.0
3 20.0
4 50.0
647 58.0
648 20.0
649 46.0
650 70.0
651 70.0
Name: price_list, dtype: float64
uj5u.com熱心網友回復:
我總是用這個:
df['DataFrame Column'] = df['DataFrame Column'].astype(float)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/432389.html
