0
def f(s):
    if s['col1'] == 2:
        return s['new_column'] = s['col1']
    elif s['col2'] == 3:
        return s['new_column'] = s['col2']
    else:
        return s['new_column'] = s['col3']

This did not worked, I know np.select but I have different nested ifs and I must create a column with so many conditions. How can I do it?

Said Taxmezov
  • 67
  • 1
  • 7
  • `s['col1'] == 2` this gives a boolean series, not `True/False`. You need to use `s['col1'].eq(2).all()`. Please go through [`How to create good pandas reproducible`](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) – Ch3steR Aug 27 '20 at 18:45
  • As you suggest `np.select` seems to be the right answer. Nested logic might make it a bit more complicated to organize, but it will still be the way to go. – ALollz Aug 27 '20 at 19:03
  • There are so many ifs in excel and I can't handle them in pandas. I used to use np.select but not for multiple nested ifs... – Said Taxmezov Aug 27 '20 at 19:57
  • Perhaps if you provide a [mcve] including sample input and sample output it would help us to understand the nature of your difficulty – G. Anderson Aug 27 '20 at 20:08

0 Answers0