I have a pandas DataFrame with MultiIndex. I want to sort the values of a column, and compare values in index level0. If the value is the maximum, the id should be 1, and if the value is the secondary, the id should be 2. Finally, output its sorted id.
For example:
arrays = [['bar', 'bar','bar', 'baz', 'baz', 'foo', 'foo','foo', 'foo','qux', 'qux'],
['one', 'two', 'three','one', 'two', 'one', 'two','three', 'four', 'one', 'two']]
df = pd.DataFrame(np.random.randn(11), index=arrays,columns=['values'])
df
output:
values
bar one -1.098567
two -0.936011
three -0.654245
baz one -0.637409
two -0.439939
foo one 0.238114
two 1.146573
three -0.512294
four -0.611913
qux one -0.481083
two 0.515961
Finally, I want this:
values sort
bar one -1.098567 3
two -0.936011 2
three -0.654245 1
baz one -0.637409 2
two -0.439939 1
foo one 0.238114 2
two 1.146573 1
three -0.512294 3
four -0.611913 4
qux one -0.481083 2
two 0.515961 1
'values': it is already an attribute that lets you access the underlying NumPy arraynp.randomso that others can easily recreate your dataframe values.