Merlin Merlin - 1 year ago 440
Python Question

Compare two columns using pandas

Using this as a starting point

a = [['10', '1.2', '4.2'], ['15', '70', '0.03'], ['8', '5', '0']]
df = pd.DataFrame(a, columns=['one', 'two', 'three'])

Out[8]:
one two three
0 10 1.2 4.2
1 15 70 0.03
2 8 5 0


I want to use something like an if statement within pandas.

if df['one'] >= df['two'] and df['one'] <= df['three'] :
df['que'] = df['one']


Basically, check each row via if statement, create new column.
The docs say use
.all
but there is no example..

Answer Source

You could use apply() and do something like this

df['que'] = df.apply(lambda x : x['one'] if x['one'] >= x['two'] and x['one'] <= x['three'] else "", axis=1)

or if you prefer not to use a lambda

def que(x):
    if x['one'] >= x['two'] and x['one'] <= x['three']:
        return x['one']
    else:
        ''
df['que'] = df.apply(que, axis=1)