Home > Mobile >  how to make changes to a existing column based on multiple conditions in python csv
how to make changes to a existing column based on multiple conditions in python csv

Time:02-11

So I am working on data processing and I want to make changes to a column called "temp_coil" based on the condition in other columns.

This is the one I made and it is giving me error.

df.temp_coil[df.outdoor_temperature < 20 & df.cooling_state == 1 & df.temp_coil == 0] = 4500

I want the answer to be 4500 if outdoor_temperature column is less than 20, cooling_state column is 1 and temp_coil column itself is 0. All the conditions need to be met.

The error it is giving me is type and value error when I run it. If this is already answered please let me know, I couldn't find an example that matched my problem.

CodePudding user response:

You must use parentheses so that the truth table is unambiguous. For your example, the correct code would be:

df.temp_coil[(df.outdoor_temperature < 20) & (df.cooling_state == 1) & (df.temp_coil == 0)] = 4500

Or:

df.temp_coil[df.outdoor_temperature.lt(20) & df.cooling_state.eq(1) & df.temp_coil.eq(0)] = 4500
  • Related