Home > Blockchain >  If statement to check the length of dataframe column value
If statement to check the length of dataframe column value

Time:07-18

I'm trying create a user defined function to check the length of dataframe columns and return certain value.

Account Open Date Account Close Date
27-04-2011 NaN
NaN NaN
NaN 01-01-2012
df['Account Close Date'] = df['Account Close Date'].astype(str) 
df['Account Open Date'] = df['Account Open Date'].astype(str) 

def close_date(row):
    close_date = row['Account Close Date']
    open_date = row['Account Open Date']
    
    if len(close_date)>0:
        return close_date
    elif len(close_date) ==0 and len(open_date) >0:
        return 'OPEN'
    else:
        return ''
    
df_tvm_calc_billing['Account Close Date'] = df_tvm_calc_billing.apply(close_date, axis =1)

TypeError: object of type 'float' has no len()

What would be the best way to do this?

I tried to convert the date values to str to identify instances where values are not NaN by checking the length of the str however, its not working.

Thanks in advance!

CodePudding user response:

IIUC, you need:

s = pd.Series(np.where(df['Account Open Date'].notna(), 'OPEN', ''),
              index=df.index)
df['Account Close Date'] = df['Account Close Date'].fillna(s)

output:

  Account Open Date Account Close Date
0        27-04-2011               OPEN
1               NaN                   
2               NaN         01-01-2012

A modification of your code would be:

def close_date(row):
    close_date = row['Account Close Date']
    open_date = row['Account Open Date']
    
    if pd.notna(close_date):
        return close_date
    elif pd.notna(open_date):
        return 'OPEN'
    else:
        return ''

but this remains inefficient compared to the above vectorial solution

CodePudding user response:

You can try this, since NaN object will trigger False

def close_date(row):
    close_date = row['Account Close Date']
    open_date = row['Account Open Date']
    
    if close_date:
        return close_date
    elif open_date:
        return 'OPEN'
    else:
        return ''
  • Related