Taking into account that:
- You are talking about pandas dataframes
- Each grade is in the form
X/100
, if not - then a basic computation can be done in the lambda
function below.
you can use the apply
method and then use the astype
to convert the column to integer values:
df = df['rating'].apply(lambda x: x.split('/')[0]).astype(int))
import pandas as pd
df = pd.DataFrame.from_dict({'rating': ['90/100', '88/100', '35/100']})
df['rating'] = df['rating'].apply(lambda x: x.split('/')[0]).astype(int)
print(df)
Returns:
rating
0 90
1 88
2 35
The more generic computation if the grade is in the format X/Y
:
df['rating'] = df['rating'].apply(lambda x: (int(x.split('/')[0]) / int(x.split('/')[1])) * 100).astype(int)
Or shortly, but extremely unsafe and not a good practice at all:
df['rating'] = df['rating'].apply(lambda x: eval(x) * 100).astype(int)
You can use it if you're just "fiddling around" with Python :-)