Just yesterday, the famous Andy Hayden added this feature to version 0.13 of pandas, which will be released any day. See here for the usage example that he added to the docs.
If you prefer installing the pandas development version from source, you can use it now.
df['Measurements'] = df.reset_index().groupby('Trial').cumcount()
The following code is equivalent if it is less simple, and will work with any latest version of pandas.
grouped = df.reset_index().groupby('Trial') df['Measurements'] = grouped.apply(lambda x: Series(np.arange(len(x)), x.index))
Finally, df.set_index(['Trial', 'Measurements'], inplace=True) to get the desired result.
source share