Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在数据帧中将比率转换为浮点?_Python_Pandas - Fatal编程技术网

Python 如何在数据帧中将比率转换为浮点?

Python 如何在数据帧中将比率转换为浮点?,python,pandas,Python,Pandas,我的数据框中有一列是男女比例。我想用一个自定义函数将其转换为float,但得到了一条错误消息 这是我的数据,看起来像: 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 这是我的代码: def convertGender (x)

我的数据框中有一列是男女比例。我想用一个自定义函数将其转换为float,但得到了一条错误消息

这是我的数据,看起来像:

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
这是我的代码:

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

times['female_male_ratio'].apply(convertGender)
以下是错误消息:

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'
AttributeError回溯(最近一次调用)
在()
---->1倍【男女比例】。适用(转换性别)
C:\Users\Aslan\AppData\Local\enthught\Canopy\edm\envs\User\lib\site packages\pandas\core\series.py在应用中(self、func、convert\u dtype、args、**kwds)
2353其他:
2354值=self.asobject
->2355 mapped=lib.map\u推断(值,f,convert=convert\u数据类型)
2356
2357如果len(映射)和isinstance(映射[0],系列):
熊猫\\u libs\src\expression.pyx(熊猫\\u libs\lib.c:66645)()
性别(x)
1.性别(x):
---->2 a,b=x.split(“:”)
3 c=整数(a)/整数(b)
4返回c
AttributeError:“float”对象没有属性“split”

您正在尝试
拆分
a
NaN
。在转换器功能中过滤掉这些内容,例如:

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

您正在尝试
拆分
a
NaN
。在转换器功能中过滤掉这些内容,例如:

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

函数中的
x
看起来并不是一个字符串。打印出
x
,并找出它实际上是什么


我的猜测是,
x
已经是一个浮点数,但在本例中它只是表示为一个比率。尝试将其强制转换为浮点数,看看会发生什么。

函数中的
x
实际上不是一个字符串,看起来可能是这样的。打印出
x
,并找出它实际上是什么


我的猜测是,
x
已经是一个浮点数,但在本例中它只是表示为一个比率。尝试将其转换为浮点数,看看会发生什么。

首先,您会遇到错误,因为您试图在Nan上拆分。您可以根据onepan给出的答案检查nan条件


其次,您需要有c=float(a)/float(b),这将为33:67提供类似于0.492537的输出。但是如果你做c=int(a)/int(b),它会给你0.0分33:67。这取决于您想要什么。

首先,您会遇到错误,因为您试图在Nan上拆分。您可以根据onepan给出的答案检查nan条件

其次,您需要有c=float(a)/float(b),这将为33:67提供类似于0.492537的输出。但是如果你做c=int(a)/int(b),它会给你0.0分33:67。这取决于你想要什么