Transform a/b to numeric in pandas [duplicate]

I have a really awkward pandas DataFrame that looks kind of like this:

identifier    per_1       per_2       per_3       per_4       per_5
'something'   124/127     100/100     24/39       14/20       10/10
'camel'       121/122     150/206     300/307     11/12       0/2
 ...          ...         ...         ...         ...         ...

So, everything but the first column is a 'fraction' that's actually a string. I'd prefer them in decimal form. To access everything but the first column, I grab:

df.loc[:,df.columns != ('identifier')]

Which works fine. If I wanted to turn a single column into decimals, I could do:

df['per_1'] = df['per_1'].apply(lambda x: [float(n) for n in x.split('/')[0:2]])
df['per_1'] = df['per_1'].apply(lambda x: x[0] / x[1] if x[1] != 0 else np.nan)

I'd then have to iterate over every column that I want to do this for. This doesn't feel very pythonic to me, considering that I can actually grab every column that I want to do this for using df.loc[:,df.columns != ('identifier')]. Is there a better way to go about this?


Try the below code:

df[['identifier']].join(df.filter(like='per').apply(pd.eval))

    identifier     per_1     per_2     per_3     per_4 per_5
0  'something'  0.976378         1  0.615385       0.7     1
1      'camel'  0.991803  0.728155  0.977199  0.916667     0