Home > database >  How to access single columns in pandas for loop?
How to access single columns in pandas for loop?

Time:11-05

I've a column which has ratings like "4.1/5" I want to remove the slash (/) and it is a object type. I want to convert it to float so I'm trying to create a function to do that.

Please correct me what I'm doing wrong. I'm trying something like

def remove_slash_from_rating(ratings):
    for i in ratings:
        df[rate] = df[rate].str.replace(r'/','')

But when I'm imputing it (df["rate"] = df["rate"].apply(remove_slash_from_rating)) then I'm getting an error

NameError: name 'rate' is not defined

Please check the above post

CodePudding user response:

There is no loop, no apply necessary, use Series.str.replace for column rate:

df["rate"] = df["rate"].str.replace(r'/','')
  • Related