data.table 与 pandas
df %>% mutate(newVariable=ifelse( , , ))太方便了
不知道pandas对应的是啥
- 已编辑
这里可以使用和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'