Anon
Anon

Reputation: 1547

How do I create a new dataframe column based on conditions of another dataframe?

I want to create a new dataframe column surv with lts and non_lts labels, where for all values of the clin["OS_MONTHS"] column in the clin dataframe, label the value as lts if value is <= 2*12, else label it as non_lts.

The criteria is:

import pandas as pd

non_lts = clin[(clin["OS_MONTHS"].astype(float)<= 2*12)]
lts = clin[~clin.isin(non_lts)].dropna()

but I want it as a dataframe surv.

My attempt:

# Survival info
def survival(clin):
    if clin["OS_MONTHS"].astype(float)<= 2*12:
        val = "lts"
    else:
        val = "non-lts"
    return val

clin['SURV'] = clin.apply(survival, axis=1)

Desired output surv

surv
0 lts
1 non_lts

Upvotes: 0

Views: 39

Answers (1)

tlentali
tlentali

Reputation: 3455

You may try this one using loc :

clin.loc[clin['OS_MONTHS'] <= 2*12, 'surv'] = 'lts' 
clin.loc[clin['OS_MONTHS'] > 2*12, 'surv'] = 'non_lts' 

Upvotes: 1

Related Questions