I have a dataset with a column of string variables (~6000) describing types of baths (ie 1 Private bath, 2 shared baths etc), is there a way for me to 'find and replace' these to simplify into simply the numeric, 1 private bath replaced with an integer 1, and so on).
Currently my attempt to use the.replace function is a longwinded and inefficient method.
db["bathrooms_text"]= db["bathrooms_text"].replace("0 baths", 0)
db["bathrooms_text"]= db["bathrooms_text"].replace("1.5 baths", 1.5)
db["bathrooms_text"]= db["bathrooms_text"].replace("2 baths", 2)
Use str.extract
:
db['bathrooms_text'] = db['bathrooms_text'].str.extract('(\d+\.?\d*)')
print(db)
# Output
bathrooms_text
0 0
1 1.5
2 2
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.