0

I try to create a max_time columns which in row rsvn 1 will have value of 2 (max of status == 'ss' of rsvn 1) and in row rsvn 2 will have value 6.

How can i do that with 1 row of pandas code

The original df doesn't have max_time column

Khoa Vo
  • 1
  • 1
  • Can you share your code or a runnable example and say what you already test? – Rémi.T Jul 31 '23 at 10:21
  • df['test']= df[df.status == 'ss'].groupby(['rsvn','status'])['time'].transform(np.max) when test this code the row where status == 'f' is all null – Khoa Vo Jul 31 '23 at 10:36

1 Answers1

1
# Assuming the data is like this:
df = pd.DataFrame(
    {'rsvn': {0: 1, 1: 1, 2: 1, 3: 1, 4: 2, 5: 2, 6: 2, 7: 2}, 
     'status': {0: 'ss', 1: 'ss', 2: 'f', 3: 'f', 4: 'ss', 5: 'ss', 6: 'f', 7: 'f'}, 
     'time': {0: 1, 1: 2, 2: 3, 3: 4, 4: 5, 5: 6, 6: 7, 7: 8}}
)

# you can recreate the max_time column like this:
max_df = df[df['status'] == 'ss'].groupby('rsvn')['time'].max().rename('max_time').reset_index()
df.merge(max_df, on='rsvn', how = 'left')
Mark
  • 7,785
  • 2
  • 14
  • 34