df %>% mutate(newVariable=ifelse( , , ))太方便了
不知道pandas对应的是啥

    kds222

    data.table的对应写法是:

    df[ , newVariable := ifelse(,,) ]

    kds222

    import pandas as pd
    df=pd.DataFrame({'name':['A','B','C'],'grade':[55,60,65]})
    df['type']=df['grade'].map(lambda x:'failed' if x<60  else 'passed')

      kds222
      9-5-2-7

      这里可以使用和ifelse()功能相同的np.where()来解决:

      import pandas as pd
      import numpy as np
      df = pd.DataFrame({'name':['A','B','C'],'grade':[55,60,65]})
      df['type'] = np.where(df['grade'] < 60, 'fail', 'pass')

      也可以使用pandas自带的重编码方法:

      df['type'][df['grade'] < 60] = 'fail'
      df['type'][df['grade'] >= 60] = 'pass' 

      或:

      df.loc[df['grade'] < 60, 'type'] = 'fail'
      df.loc[df['grade'] >= 60, 'type'] = 'pass'