Add recurring index to pandas multi indexed dataframe while keeping the order -
i add new column dataframe belwow name new_col contain number of entry in last index (while keeping order or ordering them ordered update time each sub group). result should below show in new_col.
update_time new_col book_name business_date measure_def_id 144 02-nov-16 12:00:00 pnl 02-nov-16 05:30:24 1 03-nov-16 05:30:29 2 04-nov-16 04:38:46 3 risk 02-nov-16 09:32:26 1 03-nov-16 09:31:49 2 03-nov-16 11:08:17 3
i think need set_index
:
print (df.set_index('new_col', append=true)) update_time book_name business_date measure_def_id new_col 144 02-nov-16 12:00:00 pnl 1 02-nov-16 05:30:24 2 03-nov-16 05:30:29 3 04-nov-16 04:38:46 risk 1 02-nov-16 09:32:26 2 03-nov-16 09:31:49 3 03-nov-16 11:08:17
you need cumcount
creating series
last level of index
:
print (df.set_index(df.groupby(level=[0,1,2]).cumcount() + 1, append=true))
Comments
Post a Comment