I've a dataframe with multi-level index with 9 million rows as follows. I wish to change the 0 level index which is non-unique in some cases to numerical order containing unique indices. Here's sample input:
ORDER NO DATE
4200000303 1 2015-02-19
2 2015-02-19
3 2015-02-19
1200000303 1 2016-07-19
2 2016-07-19
3 2016-07-19
4 2016-07-19
1500000303 1 2017-08-19
2 2017-08-19
3 2017-08-19
4 2017-08-19
x8594345 rows
The desired output should be:
ORDER NO DATE
0 1 2015-02-19
2 2015-02-19
3 2015-02-19
1 1 2016-07-19
2 2016-07-19
3 2016-07-19
4 2016-07-19
2 1 2017-08-19
2 2017-08-19
3 2017-08-19
4 2017-08-19
x8594345 rows
I've tried the following code:
transactions.index = pd.MultiIndex.from_arrays(
[list(range(0,transactions.shape[0])), transactions.groupby(level=0).cumcount()],
names=['ORDER', 'NO'])
But it's returning a flattened dataframe.