Home > Blockchain >  How to rewrite this expression with iloc
How to rewrite this expression with iloc

Time:10-01

I have an expression

df_1['result'][df_1['result'].isnull() & df_1['id'].notnull() ] = df_1['M'][df_1['result'].isnull() & df_1['id'].notnull()].apply(lambda x: [0]*len(x))

I get the following mistake A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead

I am trying to rewrite it like this

df_1.iloc[:,'result'][df_1.iloc[:,result].isnull() & df_1.iloc[:,'id'].notnull()] = df_1.iloc[:,'M'][df_1.iloc[:,result].isnull() & df_1.iloc[:,'id'].notnull()].apply(lambda x: [0]*len(x))

I get the following mistake

only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices

But as far as I know I can call columns by name,not by index

CodePudding user response:

You should use loc:

df_1.loc[df_1['result'].isnull() & df_1['id'].notnull(), 'result'] = df_1.loc[df_1['result'].isnull() & df_1['id'].notnull(), 'M'].apply(lambda x: [0]*len(x))

Or if that doesn't work add df_1 = df_1.copy() before the above line.

  • Related