my df looks like this:
student_id, coll_type, grad_year, completed
1001 2-year NA N
1001 2-year NA N
1001 2-year 2003 Y
1001 2-year 2004 Y
1001 4-year 2007 Y
1002 4-year NA N
1002 2-year NA N
1002 2-year NA N
1003 4-year NA N
1003 4-year NA N
1004 2-year NA N
1004 4-year 2005 Y
1004 4-year 2007 Y
My goal is get this:
student_id, coll_type, year_year, completed
1001 2-year 2003 Y
1001 4-year 2007 Y
1002 4-year NA N
1002 2-year NA N
1003 4-year NA N
1004 2-year NA N
1004 4-year 2005 Y
In summary:
- The goal is to group by
['student_id', 'coll_type']and thenaggregrateby the earliest'grad_year'(if any) and "choose" its corresponding'completed'value. - if the
'grad_year'value is "only"NAfor that group['student_id', 'coll_type'], then we selectNAand its corresponding'completed'value, which is alwaysN
What did I do?
df.groupby(['student_id', 'coll_type'])
.agg({
grad_year = ('grad_year', 'min'),
completed = ('completed', 'max')
})
but it does not work, since I am choosing different min and max value.