Excel
VLOOKUP函数
=VLOOKUP(A2,Scores!A2:B21,2,0)#不做近似填充
pandas
merge函数
方法一
students=pd.read_excel('016/Student_Score.xlsx',sheet_name='Students') scores=pd.read_excel('016/Student_Score.xlsx',sheet_name='Scores') table=students.merge(scores,on='ID')
方法二
如果!!ID设为索引后会从普通列中消失
students=pd.read_excel('016/Student_Score.xlsx',sheet_name='Students',index_col='ID') scores=pd.read_excel('016/Student_Score.xlsx',sheet_name='Scores',index_col='ID') table=students.merge(scores,how='left',left_on=students.index,right_on=scores.index).fillna(0) table.Score=table.Score.astype(int)
默认状态,在scores中查不到的数据,自动舍掉
table=students.merge(scores,on='ID')
结果:
保留students中所有数据
table=students.merge(scores,how='left',on='ID').fillna(0)
结果:
假设两张表中的参照列名不相同
table=students.merge(scores,left_on='ID',right_on='FD')
不写 on='ID' 默认寻找相同数据列名
join函数
自动识别索引
students=pd.read_excel('016/Student_Score.xlsx',sheet_name='Students',index_col='ID') scores=pd.read_excel('016/Student_Score.xlsx',sheet_name='Scores',index_col='ID') table=students.join(scores,how='left').fillna(0) table.Score=table.Score.astype(int)
join函数不再有
left_on='ID',right_on='FD'
|