I have the following sample dataframe
df = pd.DataFrame({'ID': [11, 12, 16, 19, 14, 9, 4, 13, 6, 18],
'Value': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]})
to which I add the percentage of the Value using
df['Percent Value'] = df.reset_index()['Value'].rank(method='dense', pct=True)
which gives
ID Value Percent Value
0 11 1 0.1
1 12 2 0.2
2 16 3 0.3
3 19 4 0.4
4 14 5 0.5
5 9 6 0.6
6 4 7 0.7
7 13 8 0.8
8 6 9 0.9
9 18 10 1.0
I then define the following percentage array
percentage = np.array([30, 40, 60, 90, 100])
My desired output is the following with an Order column where, based on the percentage array, the Value which are up to 30% gets order 1, 30-40% gets order 2, 40-60% gets order 3, 60-90% gets order 4, 90-100% gets order 5.
So the final output is
ID Value Percent Value Order
0 11 1 0.1 1
1 12 2 0.2 1
2 16 3 0.3 1
3 19 4 0.4 2
4 14 5 0.5 3
5 9 6 0.6 3
6 4 7 0.7 4
7 13 8 0.8 4
8 6 9 0.9 4
9 18 10 1.0 5
I can do this by looping over the Percent Value column and taking the index of the first value in percentage array which returns True for < condition. I am wondering if there is some simpler way in pandas where I can pass percentage array as the argument to compare the Percent Value column.
My attempt using list comprehension
df['Order'] = [1 + np.where(v <= percentage)[0][0] for v in df['Percent Value']]