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'