Vega
Vega

Reputation: 2929

How to apply a function a function on a column without "apply"?

I have a dataframe like:

A    | B   | C   | ...
1000 | 600 | 600 | productdesc
1500 | 400 | 600 | productdesc
1000 | 600 | 300 | productdesc

I want to create a new column D based on:

def get_result():
    if A > 1000 or B > 600 or C > 600:
        return 1
    else:
        return 0

Resulting in:

A    | B   | C   | ...         | D
1000 | 600 | 600 | productdesc | 0
1500 | 400 | 600 | productdesc | 1
1000 | 600 | 300 | productdesc | 0

Due to the dataframe size I can't use .apply because it's slow.

But I don't find a fast way to apply a function onto a column / create a new column based on a function with arguments based on other columns?

Upvotes: 1

Views: 41

Answers (2)

Quang Hoang
Quang Hoang

Reputation: 150735

Use:

df['D'] = (df['A'].gt(1000) | df['B'].gt(600) | df['C'].gt(600)).astype(int)

Or:

df['D'] = df.gt([1000,600,600]).any(1).astype(int)

Upvotes: 3

BENY
BENY

Reputation: 323226

Let us try any

df['D'] = df.gt({'A':1000,'B':600,'C':600}).any(1).astype(int)
#df['D']
Out[327]: 
0    0
1    1
2    0
dtype: int32

Upvotes: 3

Related Questions