Python Pandas- Merging two data frames based on an index order,df2['cumcount'] = df2.groupby('val1').cumcount()