Home > Net >  Fix the following error: ValueError: cannot set using a multi-index selection indexer with a differe
Fix the following error: ValueError: cannot set using a multi-index selection indexer with a differe

Time:06-17

I want to update the values from a column in a dataframe with the values from the same column but from a different row.

I tried the following code:

m = (dfc['year'] == 2015) & (dfc['max'] < 5.0)
n= dfc['Full_date'] == '28-8-2015'
dfc.loc[m, ['max']] = dfc.loc[n, ['max']].values

but it returned me the error from the title.

The dataframe I'm working is like that:

    min max Full_date   year    month   day
date                        
2015-08-03  3.89    4.80    3-8-2015    2015    8   3
2015-08-04  3.89    9.50    4-8-2015    2015    8   4
2015-08-05  3.89    6.50    5-8-2015    2015    8   5
2015-08-06  3.89    5.50    28-8-2015   2015    8   6

The expected output after the code execution would be:

min max Full_date   year    month   day
date                        
2015-08-03  3.89    5.50    3-8-2015    2015    8   3
2015-08-04  3.89    9.50    4-8-2015    2015    8   4
2015-08-05  3.89    6.50    5-8-2015    2015    8   5
2015-08-06  3.89    5.50    28-8-2015   2015    8   6

How can I solve it?

CodePudding user response:

The issue is that you consider the replacement value as a vector while it is a scalar. (If it was really a vector you would need to define how to map the values)

You can use:

# get replacement value
# here FIRST "max" value matching 28-8-2015 "Full_date"
val = dfc.loc[df['Full_date'].eq('28-8-2015').idxmax(), 'max']

# slice and replace
m = dfc['year'].eq(2015) & dfc['max'].lt(5)
dfc.loc[m, 'max'] = val

output:

             min  max  Full_date  year  month  day
date                                              
2015-08-03  3.89  5.5   3-8-2015  2015      8    3
2015-08-04  3.89  9.5   4-8-2015  2015      8    4
2015-08-05  3.89  6.5   5-8-2015  2015      8    5
2015-08-06  3.89  5.5  28-8-2015  2015      8    6
  • Related