Search code examples
pythonpandasconditional-statementspandas-loc

How to create a new column in python under multiple conditions?


I would like to create a new column under the following condition:

So basically I have two column Majoy car and Major housetype. I would let all the 'nocar' within Majoy car AND 'Rented' within Major housetype to merge to a new column.

My DataFrame is imd_car_house

I have tried: imd_car_house.loc[(cond1) | (cond2), :] but this is not working.


Solution

  • As the comments suggest it is unclear if you are using pandas but if you are, and your columns are 'Majoy car' and 'Major housetype' then the following should work if you want all the rows in your dataframe conforming to your requirements.

    imd_car_house[(imd_car_house['Majoy car']=='nocar')&(imd_car_house['Major housetype']=='Rented)]