Home > Enterprise >  Is there any Python function to extract mixed fraction from string as it is?
Is there any Python function to extract mixed fraction from string as it is?

Time:08-16

I have one columns with values like 3/8 in, 4 1/3 inches etc etc. I want to extract 3/8,4 1/3 as it is (without converting to float). How can I do it?

CodePudding user response:

Using Regex Expression- df['New Col'] = df['Numbers'].replace(r"[^0-9/ ]", "", regex=True)

OR

df['New Col'] = df['Numbers'].replace(r"[A-Za-z]", "", regex=True)

Code-

# Import pandas library
import pandas as pd
  
# initialize list elements
data = ["3/8 inches","4 1/3 inches"]
  
# Create the pandas DataFrame with column name is provided explicitly
df = pd.DataFrame(data, columns=['Numbers'])


df['New Col'] = df['Numbers'].replace(r"[^0-9/ ]", "", regex=True)
df

Output-

    Numbers        New Col
0   3/8 inches      3/8
1   4 1/3 inch      4 1/3

CodePudding user response:

A simple regex (no need for pandas):

>>> re.findall(r'\d{0,} ?\d \/\d ', '3 3/4 inches, 2/3 in, 13/11')
['3 3/4', ' 2/3', ' 13/11']

Only annoyance is, some matches may have a leading blank. But I believe it is simpler to deal with them later than to build a regex which takes that into account.

CodePudding user response:

You would probably just want to use a tuple or a class.

Maybe something like:

fraction = (3,4) # (dividen, divisor)

# or

class FractionalValue:
    def __init__(self, dividen, divisor):
        self.dividen = dividen
        self.divisor = divisor

fv = FractionalValue(3,4)

CodePudding user response:

Python has a fractions library in the stdlib which can get close to what you want:

from fractions import Fraction

Fraction("1/3")
# Fraction(1, 3)

But it does not cope with a number string like "4 1/3"

You would have to parse out the whole number and fraction parts

Once you have that you can do:

4   Fraction("1/3")
# Fraction(13, 3)
  • Related