Home > Net >  Changing fractional values in a column in Python( e.g from 4.1/5 -> 4.1)
Changing fractional values in a column in Python( e.g from 4.1/5 -> 4.1)

Time:12-26

I am fairly new to python hopefully I am able to explain the problem.

I have a DataFrame where the values in a column are present as 4.1/5 , 3.9/5 , 2/5 .

I want to convert these values to show just the numerators i.e 4.1 , 3.9 , 2.

rating= df['rate'].dropna()

0         4.1/5
1         4.1/5
2         3.8/5
3         3.7/5
4         3.8/5
          ...  
51709    3.7 /5
51711    2.5 /5
51712    3.6 /5
51715    4.3 /5
51716    3.4 /5
Name: rate, Length: 51717, dtype: object

How can this be done?

CodePudding user response:

You can first convert them into string if they are not. Then chop off the last 3 chars then convert back into float

df.rate.apply(lambda x: float(str(x)[:-3]) )

CodePudding user response:

I assume the items are strings. You can remove the slash and "denominator" using str.replace:

df['rate'] = df['rate'].str.replace(r' */.*', '', regex=True)

If you further want to convert to float:

df['rate'] = df['rate'].str.replace(r' */.*', '', regex=True).astype(float)

CodePudding user response:

You can also use:

df['rate'] = df['rate'].str.split('/', expand=True)[0]

CodePudding user response:

You can simply split on / and take first part of split:

ratings = df['rate'].str.split('/').str[0].astype(float)
  • Related