My pandas dataframe column which have prices are mostly in the format r'\d \.\d '
, which is what you expect. But when I try to convert it astype float, it says that I have got few numbers in the format \d \.\d \.\d
like this one '6041.60.1'.
How do I go about converting all of them in the format \d \.\d
with series.str.replace()? The expected result is '6041.60'.
CodePudding user response:
I'd recommand using .apply
df1["column"] = df1["column"].apply(lambda x: "".join("6041.60.1".rsplit(".",1)), axis = 1 )#remove the last "."
df1["column"] = df1["column"].astype("float")