I want to average the mean value like the average if function in python.
For example let's say I have a set of ranks for A,B,C,D as 'df' like this picture:
and I want the average of the dollar amounts according to the 'ranks':
For example, I want the average dollar amount of top two ranks (1 and 2)
I've tried to get boolean dataframes first
top = ranks < 3
bot = ranks >=3
and tried to loop through it but i don't know if this is the correct way to approach this. thanks alot for your help!
答案 0 :(得分:0)
ranks = pandas.DataFrame([[1,2,3,4], [1,3,2,4]], columns=['A','B', 'C', 'D'], index=['2000-01-31', '2000-02-01'])
amounts = pandas.DataFrame([[1,4,1,1], [1,1,1,1]], columns=['A','B', 'C', 'D'], index=['2000-01-31', '2000-02-01'])
top = amounts[ranks<3].mean(axis=1)
bot = amounts[ranks>=3].mean(axis=1)
amounts['top mean'] = top
amounts['bot mean'] = bot
ranks
A B C D
2000-01-31 1 2 3 4
2000-02-01 1 3 2 4
amounts
A B C D top mean bot mean
2000-01-31 1 4 1 1 2.5 1.0
2000-02-01 1 1 1 1 1.0 1.0