aaron
aaron

Reputation: 41

Python Pandas Group by and add new row with number sequence

 d = {'ID': ['H1', 'H1', 'H2', 'H2', 'H3', 'H3'], 'Year': ['2012', '2013', '2014', '2013', '2014', '2015'], 'Unit': [5, 10, 15, 7, 15, 20]}

df_input= pd.DataFrame(data=d)
df_input

Expected Output:

d = {'ID': ['H1', 'H1', 'H2', 'H2', 'H3', 'H3'], 'Year': ['2012', '2013', '2014', '2013', '2014', '2015'], 'Unit': [5, 10, 15, 7, 15, 20], 'lag': [0, 1, 2, 0, 1, 2], 'lag_u': [5, 5, 5, 7, 7, 7]}

df_output= pd.DataFrame(data=d)
df_output

Upvotes: 0

Views: 49

Answers (1)

jezrael
jezrael

Reputation: 862701

IIUC need GroupBy.cumcount with GroupBy.transform and GroupBy.first:

g = df_input.groupby('ID')
#if need group by both columns
#g = df_input.groupby(['ID','Year'])

df_input['lag'] = g.cumcount()
df_input['lag_u'] = g['Unit'].transform('first')

Upvotes: 1

Related Questions