0

Here is my problem. I have got this Excel file, when I import it by pandas I got this DF:

   one  two     three
0   12  0.0     NaN
1   34  NaN     NaN
2   56  34.0    NaN
3   67  5.0     NaN
4   45  0.0     NaN

Now I would like to apply function that checks:

if 'two' > 0:
    then 'three' = 'two'
else
    'three' = 'one'

Here is full code:

import pandas as pd
df = pd.read_excel('test.xlsx')
df.fillna(0.0, inplace=True)
def func(one, two):
    three = one
    if two > 0:
        three = two
    return three
df['three'] = df.apply(func(['one'], ['two']), axis='columns')

But I get back this error message:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-13-f84fe01cb9a5> in <module>
----> 1 df['three'] = df.apply(func(['one'], ['two']), axis='columns')

<ipython-input-11-7cfcd0a409d1> in func(one, two)
      1 def func(one, two):
      2     three = one
----> 3     if two > 0:
      4         three = two
      5     return three

TypeError: '>' not supported between instances of 'list' and 'int'

What is wrong? How can I change list to int? Thank you!

wotesi
  • 319
  • 1
  • 3
  • 15

1 Answers1

1

try this, np.where

df['three'] = np.where(df['two'] > 0, 'two', 'one')
sushanth
  • 8,275
  • 3
  • 17
  • 28