Reputation: 1
I am trying to take all columns with a '%' and removing the '%,' turning the string into a float, and dividing by 100, then turning it into a decimal.
I created a list of all the columns that I want to do that to with:
percentages = (df.filter(like='%').columns)
perclist = percentages.tolist()
Then I run this code but it won't work:
df[perclist] = df[perclist].str.rstrip('%').astype('float') / 100.0
Upvotes: 0
Views: 2086
Reputation: 120391
str.rstrip
is for Series
not DataFrame
.
df[perclist] = df[perclist].replace('%$', '', regex=True).astype('float') / 100.0
Tip: avoid to create a useless subset of your dataframe:
Replace:
percentages = (df.filter(like='%').columns)
perclist = percentages.tolist()
By:
perclist = df.columns[df.columns.str.contains('%')]
Upvotes: 1