0

I have the following code:

asset = df['asset_name']
diamond = asset.str.contains('DIAMOND OFFSHORE DRILLING INC')
cliffs = asset.str.contains('CLIFFS NATURAL RESOURCES INC')
deu = asset.str.contains('DEUTSCHE BANK AG')
df = asset.str.contains('(NETHERLANDS)')
pet = asset.str.contains('PETROBRAS INTERNATIONAL FINANCE COMPANY')
pet2 = asset.str.contains('PETROBRAS GLOBAL')

df['asset_name'] = np.where(diamond, 'DIAMOND OFFSHORE DRILLING INC',
                            np.where(cliffs, 'CLIFFS NATURAL RESOURCES INC',
                                     np.where(deu, 'DEUTSCHE BANK AG',
                                              np.where(df, 'DEUTSCHE FINANCE (NETHERLANDS) BV',
                                                       np.where(pet, 'PETROBRAS INTERNATIONAL FINANCE COMPANY SA',
                                                               np.where(pet2, 'PETROBAS GLOBAL FINANCE BV'))))))

But I am getting this error message:

ValueError: either both or neither of x and y should be given

I'm quite new to python and I have no idea what I've done wrong here. Any help would be much appreciated!

Lauren95
  • 1
  • 3
  • Very last `np.where` needs an if false condition. But look into `np.select` to avoid nesting. – Parfait Sep 08 '21 at 13:53
  • @Parfait yeah I eventually realised that was the problem! I was also being an idiot in that I'd already defined my DataFrame as df and used the same name again for something else. Thanks :) – Lauren95 Sep 09 '21 at 12:36
  • Consider avoiding common names like `df`, `arry`, `dct`, `lst` for object names and use natural languages to denote content of data. – Parfait Sep 09 '21 at 15:37

0 Answers0