'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 |
Solution 1:[1]
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'
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | tlentali |
