df:apply添加新列

希望各位朋友注意自己、亲人、朋友的血清尿酸水平。

import pandas as pd

df = pd.DataFrame({'id': [1, 2, 3, 4],
                   'sex': ['female', 'female', 'male', 'male'],
                   'UA': [350, 370, 400, 430]})

# df为:
   id     sex   UA
0   1  female  350
1   2  female  370
2   3    male  400
3   4    male  430


"""自定义函数来判断高尿酸血症:
女性血清尿酸水平>=360 μmol/L,
男性血清尿酸水平>=420 μmol/L"""

def HUA(i):
    if i['sex']=='female':
        if i['UA'] >= 360:
            return 'HUA'
        else:
            return 'non-HUA'
        
    else:
        if i['UA'] >= 420:
            return 'HUA'
        else:
            return 'non-HUA'
        
df['HUA']=df.apply(HUA,axis=1)
# df为:
   id     sex   UA      HUA
0   1  female  350  non-HUA
1   2  female  370      HUA
2   3    male  400  non-HUA
3   4    male  430      HUA       

你可能感兴趣的:(DataFrame,医学,python,pandas)