[英]Iterate over pandas column values
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).我有一个数据集,其中有一列字符串变量 (~6000) 描述了浴室类型(即 1 个私人浴室、2 个共用浴室等),有没有办法让我“查找并替换”这些变量以简化为简单的数字, 1 个私人浴室更换为 integer 1,依此类推。
Currently my attempt to use the.replace function is a longwinded and inefficient method.目前我尝试使用 the.replace function 是一种冗长且低效的方法。
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
:使用str.extract
:
db['bathrooms_text'] = db['bathrooms_text'].str.extract('(\d+\.?\d*)')
print(db)
# Output
bathrooms_text
0 0
1 1.5
2 2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.