Issue
the picture below is my list where i want to edit two cloumns for future analysis in data cleaning process:
"start_lng" and "end_lng" columns' content are dtype('O')
while running the code Bike_share_data["start_lng"].dtypes
Now i want to replace underscore (_) with the minus sign (-) and make the entire column dtypes as float.
i have tested a code separately as given below:
import pandas as pd
d =[ '_1.0', '_2.0', '_3.0']
d=[s.replace('_','-') for s in d]
print(d)
the result is ['-1.0', '-2.0', '-3.0'].
but unable to implement it on Bike_share_data["start_lng"] column. how can i do that?
Solution
You can use the str.replace()
method to perform the replacement and then use astype()
to change the data type.
# sample DataFrame with a "start_lng" column containing strings
data = {'start_lng': ['_1.0', '_2.0', '_3.0']}
Bike_share_data = pd.DataFrame(data)
# Replace underscores with minus signs & convert the column to float
Bike_share_data["start_lng"] = Bike_share_data["start_lng"].str.replace('_', '-').astype(float)
Answered By - Daqs
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.