I am trying to identify columns that contain the letter 'k', remove 'k' from the star rating column and multiply the result by 5.
When I multiply, instead of multiplying the column value, the value is repeated 5 times.
I've tried multiplying in other ways (use numpy, use * to multiply) but am getting the same result.
Any suggestions? Image of code: [1]
df_starrating ['StarRating'] = ['1.1k stars','900 stars','2.5k stars']
df_starrating['StarRating'] = df_starrating['StarRating'].str.replace('stars','')
if df_starrating['StarRating'].str.contains('k').any():
df_starrating['StarRating'] = df_starrating['StarRating'].str.replace('k','')
df_starrating['StarRating'] = df_starrating['StarRating'].multiply(5,axis = 'index')
Output:
1.1k, 1.1k, 1.1k, 1.1k, 1.1k
900,900,900,900,900
2.5,2.5,2.5,2.5,2.5
CodePudding user response:
You can use the .apply(function)
method. Something like this should work:
def reformat(s):
if s.find('k')!=-1
s=s.replace('k', '')
return float(s)*5
df_starrating['StarRating'] = df_starrating['StarRating'].apply(reformat)
CodePudding user response:
I believe you are multiplying a string. Try converting to a float first e.g.
df_starrating['StarRating'] = df_starrating['StarRating'].astype(float).multiply(5,axis = 'index')