简体   繁体   中英

Handle non-numeric data in python. Python beginner

I am trying to replace all non-numeric data in a dataframe in Jupyter Notebook.

def handle_non_numerical_data(df): cols = df.columns.values

for col in cols:
    text_digit_vals = {}
    def covnert_to_int(val):
        return text_digit_vals[val]

    if df[col].dtype != np.int64 and df[col].dtype != np.float64:
        col_contents = df[col].values.tolist()
        unique_elements = set(col_contents)
        x = 0
        for unique in unique_elements:
            if unique not in text_digit_vals:
                text_digit_vals[unique] = x
                x += 1

        df[col] = list(map(convert_to_int, df[col]))

return df

And then:

data_dropped_filled_numeric = handle_non_numerical_data(data_dropped_filled)

Error:

NameError Traceback (most recent call last) in ----> 1 data_dropped_filled_numeric = handle_non_numerical_data(data_dropped_filled)

in handle_non_numerical_data(df) 19 x += 1 20 ---> 21 df[col] = list(map(convert_to_int, df[col])) 22 23 return df

NameError: name 'convert_to_int' is not defined

New to python. Please advise. Thanks.

You have a typo in your function definition:

 def covnert_to_int(val):

Change "covnert" to "convert"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM