So I have a dataframe with the lines corresponding to different clothing items. I have two columns with prices and the syntax looks like this:
price | rrp |
---|---|
5.00 | 8.00 |
5 | 7.50 |
5.0 | 9.0 |
I want to convert these so that prices like 5.00/5.0 are converted to 5, but values like 7.50 stay the same. I’ve tried converting the values to integers, but this is not possible for values like 8.00. I’ve also tried to use:
df['price'].str.replace('.00', '').replace('.0', '')
But this results in a lot of values being empty. Anyone got a solution for me?
Advertisement
Answer
1 What you want only makes sense for strings, so convert everything to strings
2 replace
accepts regex
df = pd.DataFrame({'price':['5.00', '5', '5.0'], 'rrp':['8.00', '7.50', '9.0']}) df = df.astype(str).replace({'.00':'', '.0':''}, regex=True) print(df)