I have a dataframe with a column of strings that are a mix of whole numbers and mixed fractions. I would like to convert column 'y' to floats.
x y z
0 4 Info
1 8 1/2 Info
2 3/4 Info
3 10 Info
4 4 Info
5 6 1/4 Info
The logic I am considering is to split column 'y' by ' ' and '/' to create three separate columns that would look like this.
x base b c z
0 4 0 0 Info
1 8 1 2 Info
2 0 3 4 Info
3 10 0 0 Info
4 4 0 0 Info
5 6 1 4 Info
From here I could
def convertReplace(df):
convert = lambda x: float(x)
df['base'].apply(convert)
df['b'].apply(convert)
df['c'].apply(convert)
decimal = lambda x,y: x/y
try:
df['d'] = decimal(df['b'],df['c'])
df['y'] = df['base'] + df['d']
except:
df['y'] = df['base']
return df
This might work but I can't get the column to split using the method found here.
df = pd.DataFrame(df.y.str.split(' ',1).str.split('/',1).tolist(),columns = ['base','b','c'])
The error says it expects 3 arguments each time when it may be 1, 2, or 3. Even this thread doesn't use multiple separators.
The actual dataframe has over 400k rows. Efficiency would be great but I'm more interested in just getting it done. Is this logic correct or is there a more concise way to do this? Any help is appreciated.