4848

I have a sample data frame that looks like this.
df = pd.DataFrame (data = {'uid': [1,1,1,2,2,3], 'pagename':['home', 'blah',
'blah', 'home', 'blah', 'blah'], 'startpage': ['NA', 'NA', 'NA', 'home',
'home', 'blah'], 'date_time': [0,1,2,5,9,1]})
What I want to do is group by the UID and find the min date_time. If the startpage of the min date_time is Null (I put string 'NA' for Null) then I want to use the pagename from that row to populate the startpage column. I also want the startpage to be populated for all rows with the same UID.
This is the ending dataframe that I want.
df = pd.DataFrame (data = {'uid': [1,1,1,2,2,3], 'pagename':['home', 'blah',
'blah', 'home', 'blah', 'blah'], 'startpage': ['home', 'home', 'home',
'home', 'home', 'blah'], 'date_time': [0,1,2,5,9,1]})
Answer1:
fillna
with transform
i = df.groupby('uid').date_time.transform('idxmin')
df.startpage = df.startpage.fillna(i.map(df.pagename))
print(df)
date_time pagename startpage uid
0 0 home home 1
1 1 blah home 1
2 2 blah home 1
3 5 home home 2
4 9 blah home 2
5 1 blah blah 3