How to use if else in pandas numpy when apply function in all the rows fast

Viewed 100

I have a dataframe df_ia:

    dod1    dod2
0   0       0
1   200806  0
2   200806  0
3   200806  0
4   200806  0
5   200806  0
6   200806  0
7   200806  0

and a function used to apply to every row:

def life_status(dod1, dod2):
    if dod1.any() == 0:
        ls1 = '1'
    else:
        ls1 = '0'
    if dod2.any() == 0:
        ls2 = '1'
    else:
        ls2 = '0'
    lifestatus = ls1 + ls2
    return lifestatus

df_ia['lifestatus'] = life_status(df_ia['dod1'].values,df_ia['dod2'].values)

But I found that,I can't direct use :

if dod1.any() to add condition

so I tried something like,

if np.any(dod1==0):
   ls1='1'

But it still not work.

The output should looks like:

    dod1  dod2 lifestatus
0   0       0   11
1   200806  0   01
2   200806  0   01
3   200806  0   01
4   200806  0   01
5   200806  0   01
6   200806  0   01
7   200806  0   01
8   200806  0   01
9   200806  0   01

I can use this code to achieve this,

def life_status(row):
    if row['dod1'] == 0:
        ls1 = '1'
    else:
        ls1 = '0'
    if row['dod2'] == 0:
        ls2 = '1'
    else:
        ls2 = '0'
    lifestatus = ls1 + ls2
    return lifestatus
df['lifestatus'] = df.apply(lambda row: life_status(row), axis=1)

but it is very slow that is why I post this question.

1 Answers

Solution

Based on what you explained in the comment's section, your previously shared function had a wrong logic and that misguided my previous solution. You need to evaluate int(dod1[i] == 0) + int(dod2[i] == 0) for each row and return a series or numpy.ndarray.

import numpy as np
import pandas as pd

df = pd.DataFrame({
    'dod1': [0] + [200806 for _ in range(7)], 
    'dod2': [0 for _ in range(8)],
})

def life_status(dod1: np.ndarray, dod2: np.ndarray):
    return (dod1 == 0).astype(int).astype(str) + (dod2 == 0).astype(int).astype(str)

life_status(df['dod1'].values, df['dod2'].values)

## Output:
# I will update this later. But the function should work as expected. 

Or, equivalently, directly use this on the dataframe.

(df.dod1 == 0).astype(int).astype(str) + (df.dod2 == 0).astype(int).astype(str)

A Note to the reader

In case you want to make it more generic, such as when (dod1 == 0) is True, assign 4 and when it is False, assigne 5, you can do it as follows.

# schema:
# - condition: dod1 == 0 --> True: 4, False: 5
# - condition: dod1 == 0 --> True: 7, False: 8
cond1, cond2 = (df.dod1 == 0), (df.dod2 == 0)
((cond1 * 4 + ~cond1 * 5).astype(str) + (cond2 * 7 + ~cond2 * 8).astype(str)).tolist()

## Output
# ['47', '57', '57', '57', '57', '57', '57', '57']

You can further improvise it and allow any value (str, int, float) to replace based on when it is True or False.

(df.dod1 == 0).astype(str).replace({'True': '4', 'False': '5'}) + \
(df.dod2 == 0).astype(str).replace({'True': '7', 'False': '8'})

## Output
# ['47', '57', '57', '57', '57', '57', '57', '57']
Related