1

I want to remove the "HKD $" and change to Float object

I use below command line but it doesnt work.

df['price_list_1'] = df['price_list_1'].str.replace("HKD $","")

The result:

/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

2 Answers 2

2

When you use .str.replace(), you should be care that if the string value includes any special characters of regular expressions such as $, ^, and *, in this case, $.

To explicity say '$' is not for regular expression, you can use regex=False.

df['price_list_1'] = df['price_list_1'].str.replace("HKD $","", regex=False)

For your information about using regular expression in str.replace: see https://datascientyst.com/replace-values-regex-pandas/

Sign up to request clarification or add additional context in comments.

Comments

1

You could also use str.lstrip:

out = df['price_list'].str.lstrip('$').astype(float)

Output:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.