students = pd.read_excel(r'D:\Student_score.xlsx', sheet_name='Students')
scores = pd.read_excel(r'D:\Student_score.xlsx', sheet_name='Scores')
横向联合用merge方法
on=‘ID’,用‘ID’列合并
how=‘left’,保留左列的所有元素
.fillna(0),NaN填0
table = students.merge(scores, how='left', on='ID').fillna(0)
设置那些0位整型
table.Score = table.Score.astype(int)
jion函数,和merge几乎一样,默认用index联合,而merge不能用index联合。
老师推介用join,但查McKinney书,并无join介绍。
table = students.join(scores, how='left').fillna(0)
table.Score = table.Score.astype(int)