Yomibrain
Yomibrain

Reputation: 3

Converting a string to number in jupyter

Here is my code:

def value_and_wage_conversion(value):
    if isinstance(value,str):
        if 'M' in out:
            out = float(out.replace('M', ''))*1000000
        elif 'K' in value:
            out = float(out.replace('K', ''))*1000
        return float(out)

fifa_18['Value'] = fifa_18['Value'].apply(lambda x: value_and_wage_conversion(x))
fifa_18['Wage'] = fifa_18['Wage'].apply(lambda x: value_and_wage_conversion(x))

Here is the error message:

--------------------------------------------------------------------------- UnboundLocalError Traceback (most recent call last) in 7 return float(out) 8 ----> 9 fifa_18['Value'] = fifa_18['Value'].apply(lambda x: value_and_wage_conversion(x)) 10 fifa_18['Wage'] = fifa_18['Wage'].apply(lambda x: value_and_wage_conversion(x))

c:\users\brain\appdata\local\programs\python\python39\lib\site-packages\pandas\core\series.py in apply(self, func, convert_dtype, args, **kwds) 4136
else: 4137 values = self.astype(object)._values -> 4138 mapped = lib.map_infer(values, f, convert=convert_dtype) 4139 4140 if len(mapped) and isinstance(mapped[0], Series):

pandas_libs\lib.pyx in pandas._libs.lib.map_infer()

in (x) 7 return float(out) 8 ----> 9 fifa_18['Value'] = fifa_18['Value'].apply(lambda x: value_and_wage_conversion(x)) 10 fifa_18['Wage'] = fifa_18['Wage'].apply(lambda x: value_and_wage_conversion(x))

in value_and_wage_conversion(value) 1 def value_and_wage_conversion(value): 2 if isinstance(value,str): ----> 3 if 'M' in out: 4 out = float(out.replace('M', ''))*1000000 5 elif 'K' in value:

UnboundLocalError: local variable 'out' referenced before assignment

Upvotes: 0

Views: 688

Answers (1)

Max Pierini
Max Pierini

Reputation: 2249

You were almost there but you need to fix your function

For example

import numpy as np
import pandas as pd

# generate a random sample
values = ['10M', '10K', 10.5, '200M', '200K', 200]
size = 100
np.random.seed(1)
df = pd.DataFrame({
    'Value': np.random.choice(values, size),
    'Wage': np.random.choice(values, size),
})

print(df)

   Value  Wage
0    200   200
1   200M  200M
2   200K   200
3    10M   10M
4    10K  200M
..   ...   ...
95  200K   200
96   200  200M
97  10.5  200K
98  200K  10.5
99  200M   10M

[100 rows x 2 columns]

Define function and apply

def value_and_wage_conversion(value):
    if isinstance(value, str):
        if 'M' in value:
            value = float(value.replace('M', ''))*1000000
        elif 'K' in value:
            value = float(value.replace('K', ''))*1000
        return float(value)

df['Value'] = df['Value'].apply(lambda x: value_and_wage_conversion(x))
df['Wage'] = df['Wage'].apply(lambda x: value_and_wage_conversion(x))

print(df)

          Value         Wage
0         200.0        200.0
1   200000000.0  200000000.0
2      200000.0        200.0
3    10000000.0   10000000.0
4       10000.0  200000000.0
..          ...          ...
95     200000.0        200.0
96        200.0  200000000.0
97         10.5     200000.0
98     200000.0         10.5
99  200000000.0   10000000.0

[100 rows x 2 columns]

and check

df.info()

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 100 entries, 0 to 99
Data columns (total 2 columns):
 #   Column  Non-Null Count  Dtype  
---  ------  --------------  -----  
 0   Value   100 non-null    float64
 1   Wage    100 non-null    float64
dtypes: float64(2)
memory usage: 1.7 KB

Upvotes: 1

Related Questions