Home > database >  Creating a function to set 'nan' values
Creating a function to set 'nan' values

Time:10-13

I am trying to create a function to set nan values specific columns as follows:

 def set_nan_for_specific_column(table, column):
    result = table[str(column)] = np.nan
    return result

merged_table['code'] = set_nan_for_specific_column(merged_table, code)

I get the following error:

 NameError: name 'code' is not defined

any suggestions anyone?

CodePudding user response:

From what i see the problem comes from this line:

merged_table['code'] = set_nan_for_specific_column(merged_table, code) 

Indeed you give to the function the argument code which has never been defined before and then leading to an error. Even if you change the dtype of code afterwards to obtain 'col' in order to select the columns named code from your df, when calling the fucntion set_nan_for_specific_column , it will search for a variable named col, which does not exist.

CodePudding user response:

the parameters code, that I put between stars is not defined:

    merged_table['code'] = set_nan_for_specific_column(merged_table, **code**)

Moreover, notice that in this way you will add two new columns to your dataframe. One is "code", the other one will be the value contained inside the variable code. for example:

    code = "z"
    df['code'] = set_nan_for_specific_column(df, code)

Will give you these 2 new columns: z and code

  • Related