处理python中的非数字数据。 Python初学者

问题描述 投票:0回答:1

我正在尝试替换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

然后:

data_dropped_filled_numeric = handle_non_numerical_data(data_dropped_filled)

错误:

NameError Traceback(最近一次通话)在----> 1个data_dropped_filled_numeric = handle_non_numerical_data(data_dropped_filled)

在handle_non_numerical_data(df)中19 x + = 120---> 21 df [col] = list(map(convert_to_int,df [col]))2223 return df

NameError:未定义名称'convert_to_int'

是python的新手。请指教。谢谢。

python syntax
1个回答
1
投票

您的函数定义中有错字:

 def covnert_to_int(val):
© www.soinside.com 2019 - 2024. All rights reserved.