Reputation: 91
I have the following data frame:
vote_prprtn uniform_votesh p_by_q ln_p_by_q p_ln_p_by_q match
0.0116 0.009367 1.238434 0.213848 0.002481 False
0.0100 0.009367 1.067616 0.065428 0.000654 True
0.0065 0.009367 0.693950 -0.365355 -0.002375 True
0.0072 0.006000 1.200000 0.182321 0.001313 False
0.0048 0.006000 0.800000 -0.223143 -0.001071 True
I want to create another column which has the row value of p_ln_p_by_q when match = False and does a cumulative sum, row by row when match is True. This continues until the next False value. My final dataframe should look like this:
vote_prprtn uniform_votesh p_by_q ln_p_by_q p_ln_p_by_q match final_val
0.0116 0.009367 1.238434 0.213848 0.002481 False 0.002481
0.0100 0.009367 1.067616 0.065428 0.000654 True 0.003135
0.0065 0.009367 0.693950 -0.365355 -0.002375 True 0.00076
0.0072 0.006000 1.200000 0.182321 0.001313 False 0.001313
0.0048 0.006000 0.800000 -0.223143 -0.001071 True 0.000242
Any help on this would genuinely make me understand the parallels between excel and pandas.
Upvotes: 1
Views: 1449
Reputation: 4021
groupby
using a group defined by each time match==False
then do the cumsum
over each group.
df['final_val'] = df.groupby((~df.match).cumsum())['p_ln_p_by_q'].cumsum()
df['final_val']
# 0 0.002481
# 1 0.003135
# 2 0.000760
# 3 0.001313
# 4 0.000242
Upvotes: 3
Reputation: 95
I don't know how your data frame is done, but you can try something in this style :
sum = 0
for row in frame.rows:
if(row.match == False):
sum = 0
sum += row.p_ln_p_by_q
row.final_val = sum
(Take care to adapt the way you read/write in your row, this is just a Python-based pseudo-code).
Upvotes: 1