将函数应用于 Pandas returns UnboundLocalError 中的数据框

Applying function to a data frame in Pandas returns UnboundLocalError

我的数据框如下所示:

        Name    Overall Rating  Value in Millions
0   Neymar Jr   92  €105.5M
1   L. Messi    94  €95.5M
2   K. Mbappé   89  €93.5M
3   V. van Dijk     91  €90M
4   K. De Bruyne    91  €90M
...     ...     ...     ...
19692   I. Isa  63  €0
19693   I. Fetfatzidis  74  €0
19694   M. Mohsen   66  €0
19695   B. Jokič    72  €0
19696   B. Sigurðarson  73  €0

我正在尝试对第 3 列“以百万计的价值”应用一个函数,以将值从字符串格式转换为浮点数:

#A function to convert the values in the third row from strings to floats
    
def value_to_float(value_as_string): # eg.'€95.5M'
    
    value_as_string = value_as_string.strip('€')   
    
    if 'M' in value_as_string:                       #95.5M - string
        value_as_string = value_as_string.strip('M') #95.5 - string
        multiplier = float(value_as_string)          #95.5 - float
        value_as_float = multiplier * 1000000        #95000000.0 - float
        
    if 'K' in value_as_string:
        value_as_string = value_as_string.strip('K') 
        multiplier = float(value_as_string)
        value_as_float = multiplier * 1000           #Same as above, in case of K(Thousands)
        
    return value_as_float

当给定显式参数时,该函数可以正常工作:

value_to_float('€95.5M')

95500000.0

但是,当我尝试以下操作时:

players["Value in Millions"].apply(value_to_float)

我收到这个错误:

---------------------------------------------------------------------------
UnboundLocalError                         Traceback (most recent call last)
<ipython-input-80-3d3345f9405d> in <module>
----> 1 players["Value in Millions"].apply(value_to_float)

~/anaconda3/lib/python3.7/site-packages/pandas/core/series.py in apply(self, func, convert_dtype, args, **kwds)
   3846             else:
   3847                 values = self.astype(object).values
-> 3848                 mapped = lib.map_infer(values, f, convert=convert_dtype)
   3849 
   3850         if len(mapped) and isinstance(mapped[0], Series):

pandas/_libs/lib.pyx in pandas._libs.lib.map_infer()

<ipython-input-61-158745b17930> in value_to_float(value_as_string)
     15         value_as_float = multiplier * 1000           #Same as above, in case of K(Thousands)
     16 
---> 17     return value_as_float

UnboundLocalError: local variable 'value_as_float' referenced before assignment

我尝试了其他几种方法(series.map(),老式循环),但我总是得到同样的错误,所以我倾向于认为某处逻辑存在漏洞。

您的问题源于这样一个事实,即您的函数假定所有行在其“价值百万”的值中都有一个“M”或“K”。在上面您自己的数据框示例中,我们可以清楚地看到“百万价值”为“€0”的示例。按照函数逻辑,value_as_float 变量永远不会被设置,因此它会抛出您概述的错误。

调整您的函数以将 value_as_float 默认设置为 0 可以解决此问题。

def value_to_float(value_as_string): # eg.'€95.5M'
    
    value_as_string = value_as_string.strip('€')
    value_as_float = 0
    
    if 'M' in value_as_string:                       #95.5M - string
        value_as_string = value_as_string.strip('M') #95.5 - string
        multiplier = float(value_as_string)          #95.5 - float
        value_as_float = multiplier * 1000000        #95000000.0 - float
        
    if 'K' in value_as_string:
        value_as_string = value_as_string.strip('K') 
        multiplier = float(value_as_string)
        value_as_float = multiplier * 1000           #Same as above, in case of K(Thousands)
        
    return value_as_float