Home > OS >  Python: combining np.where with text columns
Python: combining np.where with text columns

Time:05-31

I want to insert a new column into a data frame in Python based on the below text conditions

Data['new_column'] = np.where(Data.Column1 = 'Text1', 1.20, 
                     np.where(Data.Column2  = 'Text 2' & Data.Column3='Text2' , 1.09, 1))

np.where works with numeric columns but not when there are text based. What do I need to adapt in my code to make it work?

CodePudding user response:

I found a couple problems with the code

  1. when you are doing a comparison you must use == if you want to know if something is equal
  2. for the second np.where() if you wish to use multiple arguments in a single expression they must be surrounded by ()
Data = {
    'Column1' : ['Text1', 2, 3, np.nan],
    'Column2' : [1, 'Text 2', 3, 4],
    'Column3' : [1, 'Text 2', 3, 4]
}
Data = pd.DataFrame(Data)
Data['new_column'] = np.where(Data.Column1 == 'Text1', 1.20,
                     np.where((Data.Column2  == 'Text 2') & (Data.Column3 == 'Text 2') , 1.09, 1))
Data
  • Related