Xing li
Xing li

Reputation: 1

How to convert ratio to float in Pandas Dataframe?

One column of my data frame is male_female_ratio. And I want to convert it to float with a self defined function but got a error message.

This is my data looks like:

0           NaN
1       33 : 67
2       37 : 63
3       42 : 58
4       45 : 55
5       46 : 54
6       46 : 54
7       50 : 50
8       37 : 63
9       50 : 50

This is my code:

def convertGender (x):
    a, b= x.split(':')
    c = int(a)/int(b)
    return c

times['female_male_ratio'].apply(convertGender)

Here is the error message:

AttributeError                            Traceback (most recent call last)
<ipython-input-70-ae173d1b923c> in <module>()
----> 1 times['female_male_ratio'].apply(convertGender)

C:\Users\Aslan\AppData\Local\Enthought\Canopy\edm\envs\User\lib\site-packages\pandas\core\series.py in apply(self, func, convert_dtype, args, **kwds)
   2353             else:
   2354                 values = self.asobject
-> 2355                 mapped = lib.map_infer(values, f, convert=convert_dtype)
   2356 
   2357         if len(mapped) and isinstance(mapped[0], Series):

pandas\_libs\src\inference.pyx in pandas._libs.lib.map_infer (pandas\_libs\lib.c:66645)()

<ipython-input-69-3584a8e2ceb3> in convertGender(x)
      1 def convertGender (x):
----> 2     a, b= x.split(':')
      3     c = int(a)/int(b)
      4     return c

AttributeError: 'float' object has no attribute 'split'

Upvotes: 0

Views: 425

Answers (3)

Humi
Humi

Reputation: 609

first, you are getting that error because you are trying to split on Nan. You can check for nan condition as per the answer given by onepan.

Second, you need to have c=float(a)/float(b) which will give you output like 0.492537 for 33:67. But if you do c=int(a)/int(b) it will give you 0.0 for 33:67. Really depends on what you want.

Upvotes: 0

Stephen C
Stephen C

Reputation: 2036

It looks like x in your function is not actually a string like it looks like it might be. Print out x and figure out what it actually is.

My guess is that x is already a float, but it's just represented as a ratio in this case. Try casting it to a float and see what happens.

Upvotes: 0

onepan
onepan

Reputation: 954

you're trying to split a NaN. Filter those out in your converter function, e.g.:

def convertGender (x):
    if x is np.nan:
        return np.nan
    a, b = x.split(':') 
    c = int(a)/int(b) 
    return c 

Upvotes: 2

Related Questions