Issue
I've created a table using pandas.groupby, but want to add a column that does a calculation on the columns in the groupby output. Here is my code (I'm using Python 2) and result:
DMM_pivot = df.query('rpc_flag != "IB RPC"').groupby(['rpc_flag', 'DMM_type'])
DMM_pivot = DMM_pivot['number_accounts'].sum().unstack('rpc_flag').fillna(0)
DMM_pivot = DMM_pivot.apply(lambda x: x / x.sum()).round(2) # Show percentage of total
print DMM_pivot
rpc_flag No RPC OB RPC
DMM_type
Bad 0.34 0.25
Good 0.66 0.75
I want to add a column that shows percentage difference accross the columns: (OB RPC value / No RPC value - 1) and expect these results:
rpc_flag No RPC OB RPC Change
DMM_type
Bad 0.34 0.25 -0.26
Good 0.66 0.75 0.14
Solution
Use apply.
DMM_pivot["Change"] = DMM_pivot.apply(lambda x: x["OB RPC"]/x["No RPC"] -1, axis=1)
Answered By - Igor Rivin
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.