Home > Software design >  How to grab the first row from a column and last row from another column from a group by
How to grab the first row from a column and last row from another column from a group by

Time:04-12

I have a df with 3 columns. Tenant, start and end as columns

Tenant Start End
x 10 20
x 20 30
x 30 40
y 15 30
y 30 45

I want to group by tenant and get the first value from start and last value from end to. The results df should look like this:

Tenant Start End
x 10 40
y 15 45

this is my code but its not correct:

df_login_merge_final = df_login_merge_final.groupby('tenant').first().reset_index()
df_login_summary = df_login_merge_final[['tenant','outage_start']]
df_login_merge_final_1 = df_login_merge_final.groupby('tenant').last().reset_index()
df_login_summary_1 = df_login_merge_final_1[['tenant','outage_end']]
df_login_summary_master = pd.merge(df_login_summary,df_login_summary_1,on=['tenant'],how ='inner')

CodePudding user response:

You can do it with one groupby function and applying the aggregation functions min and max on each column that you need using this syntax:

df.groupby(['Tenant']).agg({'Start': min, 'End': max})
  • Related