I have the following df.
wallet position position_rewards position_type token_transfers total_position_rewards rewards_pct transfers_per_rewards
0 0x123 FRAX 700 LD 450 1000 0.7 315
0 0x123 USDC 200 LD 450 1000 0.2 90
0 0x123 ETH 100 Sushi_LP 450 1000 0.1 45
1 0xabc FXS 40 LD 5 50 0.8 4
1 0xabc ETH 10 Sushi_LP 5 50 0.2 1
A few notes about the data:
- The values in the columns
total_transfersandtotal_position_rewardsare "aggregated" by wallet. That is, each wallet has only one value for that column, even though the wallet is repeated throughout thedf(because other values in the other columns differ).
What I want to output:
I am interested in creating a table/showing/charting the amount of token_transfers per position_type.
One wallet can have different positions, but the position_typecan be the same (see wallet 0x123).
total_position_rewards is a sum of all position_rewardsfor a given wallet and rewards_pct is basically position_rewards divided by total_position_rewards.
What I want to calculate is the total_token_transfers_per_position_type. Which means that I would need to group token_transfers per position_type, but since the values token_transfers are only one for each wallet, I cannot do df.groupby(['wallet', 'position_type'])['token_transfers'].sum() because I do not want the sum of token_transfers, but its sum across wallet and positon_type (and NOT sum these values within the wallet).
My code:
df.groupby(['wallet', 'position_type'])['token_transfers'].sum()
As stated above, I need ot somehowe ignore values being summed across wallet, only across position_type.
For example, if you do the above operation, I would get the following results for total_token_transfers_per_position_type:
LD: 450 + 450 + 5 = 905
Sushi_LP: 450 + 5 = 455
however, the correct result should be:
LD: 450 + 5 = 455
Sushi_LP: 450 + 5 = 455
Basically, I need to sum token_transfers across each position_type, but not summing them up within wallets.
If I do a df.groupby('wallet')['token_transfers'].sum() and one wallet has mulitple positon_types, I will get wrongly summed values.
If I do a df.groupby('position_type')['token_transfers'].sum() pandas will wrongly sum the values within each wallet.