How to do a calculation only at some rows of my dataframe?












-2














Let's say I have a dataframe with only two columns and 20 rows, where all values from the first column are equal to 10, and all values from the second row are random percentage numbers.



Now, I want to multiply the first column with the percentage values of the second column +1, but only at some intervals, and copy the last value to the next row.



E.g. I want to do this multiplication operation from row 5 to 10.



The problem Is that I don't know to start and end the calculation in arbitrary spots based on the df's index.



Example input data:



df = pd.DataFrame(np.random.randint(0,10,size=(20, 2)), columns=list('AB'))
df['A'] = 10
df['B'] = df['B'] /100


Which produces:



      A     B
0 10 0.07
1 10 0.02
2 10 0.05
3 10 0.00
4 10 0.01
5 10 0.09
6 10 0.00
7 10 0.02
8 10 0.03
9 10 0.05
10 10 0.05
11 10 0.03
12 10 0.01
13 10 0.09
14 10 0.06
15 10 0.07
16 10 0.01
17 10 0.01
18 10 0.01
19 10 0.07


An output I would like to get, is where the first row go thorugh a comulative multiplication only at sow rows, like this:



      C       B
0 10 0.07
1 10 0.02
2 10 0.05
3 10 0.00
4 10 0.01
5 10,9 0.09
6 10,9 0.00
7 11,11 0.02
8 11,45 0.03
9 12,02 0.05
10 12,62 0.05
11 12,62 0.03
12 12,62 0.01
13 12,62 0.09
14 12,62 0.06
15 12,62 0.07
16 12,62 0.01
17 12,62 0.01
18 12,62 0.01
19 12,62 0.07


Thank you!










share|improve this question
























  • please provide a sample of the input data you'd like to use and what you would expect as output, as your question is currently difficult to answer when we don't truly know what you want. And hey, if it suits you throw some code in there you've tried as well!
    – d_kennetz
    Nov 18 '18 at 1:47










  • I've added some example data. Unfortunately, I don't have any attempt worth sharing.
    – Arthur Coimbra
    Nov 18 '18 at 2:04
















-2














Let's say I have a dataframe with only two columns and 20 rows, where all values from the first column are equal to 10, and all values from the second row are random percentage numbers.



Now, I want to multiply the first column with the percentage values of the second column +1, but only at some intervals, and copy the last value to the next row.



E.g. I want to do this multiplication operation from row 5 to 10.



The problem Is that I don't know to start and end the calculation in arbitrary spots based on the df's index.



Example input data:



df = pd.DataFrame(np.random.randint(0,10,size=(20, 2)), columns=list('AB'))
df['A'] = 10
df['B'] = df['B'] /100


Which produces:



      A     B
0 10 0.07
1 10 0.02
2 10 0.05
3 10 0.00
4 10 0.01
5 10 0.09
6 10 0.00
7 10 0.02
8 10 0.03
9 10 0.05
10 10 0.05
11 10 0.03
12 10 0.01
13 10 0.09
14 10 0.06
15 10 0.07
16 10 0.01
17 10 0.01
18 10 0.01
19 10 0.07


An output I would like to get, is where the first row go thorugh a comulative multiplication only at sow rows, like this:



      C       B
0 10 0.07
1 10 0.02
2 10 0.05
3 10 0.00
4 10 0.01
5 10,9 0.09
6 10,9 0.00
7 11,11 0.02
8 11,45 0.03
9 12,02 0.05
10 12,62 0.05
11 12,62 0.03
12 12,62 0.01
13 12,62 0.09
14 12,62 0.06
15 12,62 0.07
16 12,62 0.01
17 12,62 0.01
18 12,62 0.01
19 12,62 0.07


Thank you!










share|improve this question
























  • please provide a sample of the input data you'd like to use and what you would expect as output, as your question is currently difficult to answer when we don't truly know what you want. And hey, if it suits you throw some code in there you've tried as well!
    – d_kennetz
    Nov 18 '18 at 1:47










  • I've added some example data. Unfortunately, I don't have any attempt worth sharing.
    – Arthur Coimbra
    Nov 18 '18 at 2:04














-2












-2








-2







Let's say I have a dataframe with only two columns and 20 rows, where all values from the first column are equal to 10, and all values from the second row are random percentage numbers.



Now, I want to multiply the first column with the percentage values of the second column +1, but only at some intervals, and copy the last value to the next row.



E.g. I want to do this multiplication operation from row 5 to 10.



The problem Is that I don't know to start and end the calculation in arbitrary spots based on the df's index.



Example input data:



df = pd.DataFrame(np.random.randint(0,10,size=(20, 2)), columns=list('AB'))
df['A'] = 10
df['B'] = df['B'] /100


Which produces:



      A     B
0 10 0.07
1 10 0.02
2 10 0.05
3 10 0.00
4 10 0.01
5 10 0.09
6 10 0.00
7 10 0.02
8 10 0.03
9 10 0.05
10 10 0.05
11 10 0.03
12 10 0.01
13 10 0.09
14 10 0.06
15 10 0.07
16 10 0.01
17 10 0.01
18 10 0.01
19 10 0.07


An output I would like to get, is where the first row go thorugh a comulative multiplication only at sow rows, like this:



      C       B
0 10 0.07
1 10 0.02
2 10 0.05
3 10 0.00
4 10 0.01
5 10,9 0.09
6 10,9 0.00
7 11,11 0.02
8 11,45 0.03
9 12,02 0.05
10 12,62 0.05
11 12,62 0.03
12 12,62 0.01
13 12,62 0.09
14 12,62 0.06
15 12,62 0.07
16 12,62 0.01
17 12,62 0.01
18 12,62 0.01
19 12,62 0.07


Thank you!










share|improve this question















Let's say I have a dataframe with only two columns and 20 rows, where all values from the first column are equal to 10, and all values from the second row are random percentage numbers.



Now, I want to multiply the first column with the percentage values of the second column +1, but only at some intervals, and copy the last value to the next row.



E.g. I want to do this multiplication operation from row 5 to 10.



The problem Is that I don't know to start and end the calculation in arbitrary spots based on the df's index.



Example input data:



df = pd.DataFrame(np.random.randint(0,10,size=(20, 2)), columns=list('AB'))
df['A'] = 10
df['B'] = df['B'] /100


Which produces:



      A     B
0 10 0.07
1 10 0.02
2 10 0.05
3 10 0.00
4 10 0.01
5 10 0.09
6 10 0.00
7 10 0.02
8 10 0.03
9 10 0.05
10 10 0.05
11 10 0.03
12 10 0.01
13 10 0.09
14 10 0.06
15 10 0.07
16 10 0.01
17 10 0.01
18 10 0.01
19 10 0.07


An output I would like to get, is where the first row go thorugh a comulative multiplication only at sow rows, like this:



      C       B
0 10 0.07
1 10 0.02
2 10 0.05
3 10 0.00
4 10 0.01
5 10,9 0.09
6 10,9 0.00
7 11,11 0.02
8 11,45 0.03
9 12,02 0.05
10 12,62 0.05
11 12,62 0.03
12 12,62 0.01
13 12,62 0.09
14 12,62 0.06
15 12,62 0.07
16 12,62 0.01
17 12,62 0.01
18 12,62 0.01
19 12,62 0.07


Thank you!







python python-3.x pandas dataframe






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 18 '18 at 3:22

























asked Nov 18 '18 at 1:45









Arthur Coimbra

194




194












  • please provide a sample of the input data you'd like to use and what you would expect as output, as your question is currently difficult to answer when we don't truly know what you want. And hey, if it suits you throw some code in there you've tried as well!
    – d_kennetz
    Nov 18 '18 at 1:47










  • I've added some example data. Unfortunately, I don't have any attempt worth sharing.
    – Arthur Coimbra
    Nov 18 '18 at 2:04


















  • please provide a sample of the input data you'd like to use and what you would expect as output, as your question is currently difficult to answer when we don't truly know what you want. And hey, if it suits you throw some code in there you've tried as well!
    – d_kennetz
    Nov 18 '18 at 1:47










  • I've added some example data. Unfortunately, I don't have any attempt worth sharing.
    – Arthur Coimbra
    Nov 18 '18 at 2:04
















please provide a sample of the input data you'd like to use and what you would expect as output, as your question is currently difficult to answer when we don't truly know what you want. And hey, if it suits you throw some code in there you've tried as well!
– d_kennetz
Nov 18 '18 at 1:47




please provide a sample of the input data you'd like to use and what you would expect as output, as your question is currently difficult to answer when we don't truly know what you want. And hey, if it suits you throw some code in there you've tried as well!
– d_kennetz
Nov 18 '18 at 1:47












I've added some example data. Unfortunately, I don't have any attempt worth sharing.
– Arthur Coimbra
Nov 18 '18 at 2:04




I've added some example data. Unfortunately, I don't have any attempt worth sharing.
– Arthur Coimbra
Nov 18 '18 at 2:04












1 Answer
1






active

oldest

votes


















2














To get the recursive product you can do the following:



start = 5
end = 10

df['C'] = ((1+df.B)[start:end+1].cumprod().reindex(df.index[:end+1]).fillna(1)*df.A).ffill()


Output:



     A     B          C
0 10 0.07 10.000000
1 10 0.02 10.000000
2 10 0.05 10.000000
3 10 0.00 10.000000
4 10 0.01 10.000000
5 10 0.09 10.900000
6 10 0.00 10.900000
7 10 0.02 11.118000
8 10 0.03 11.451540
9 10 0.05 12.024117
10 10 0.05 12.625323
11 10 0.03 12.625323
12 10 0.01 12.625323
13 10 0.09 12.625323
14 10 0.06 12.625323
15 10 0.07 12.625323
16 10 0.01 12.625323
17 10 0.01 12.625323
18 10 0.01 12.625323
19 10 0.07 12.625323




Explanation:



Calculate the cumulative product of (1 + df.B), which is the factor to mulitply by df.A to obtain the recursive product. Do this only over the range specified. reindex and fill the the rows before start with 1, so the value remains constant before this range.



Multiply by df.A to get the actual value, forward filling values after the range you specify.






share|improve this answer



















  • 1




    This's exactly it! It works perfectly when using more than one range.
    – Arthur Coimbra
    Nov 18 '18 at 6:54











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53357204%2fhow-to-do-a-calculation-only-at-some-rows-of-my-dataframe%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














To get the recursive product you can do the following:



start = 5
end = 10

df['C'] = ((1+df.B)[start:end+1].cumprod().reindex(df.index[:end+1]).fillna(1)*df.A).ffill()


Output:



     A     B          C
0 10 0.07 10.000000
1 10 0.02 10.000000
2 10 0.05 10.000000
3 10 0.00 10.000000
4 10 0.01 10.000000
5 10 0.09 10.900000
6 10 0.00 10.900000
7 10 0.02 11.118000
8 10 0.03 11.451540
9 10 0.05 12.024117
10 10 0.05 12.625323
11 10 0.03 12.625323
12 10 0.01 12.625323
13 10 0.09 12.625323
14 10 0.06 12.625323
15 10 0.07 12.625323
16 10 0.01 12.625323
17 10 0.01 12.625323
18 10 0.01 12.625323
19 10 0.07 12.625323




Explanation:



Calculate the cumulative product of (1 + df.B), which is the factor to mulitply by df.A to obtain the recursive product. Do this only over the range specified. reindex and fill the the rows before start with 1, so the value remains constant before this range.



Multiply by df.A to get the actual value, forward filling values after the range you specify.






share|improve this answer



















  • 1




    This's exactly it! It works perfectly when using more than one range.
    – Arthur Coimbra
    Nov 18 '18 at 6:54
















2














To get the recursive product you can do the following:



start = 5
end = 10

df['C'] = ((1+df.B)[start:end+1].cumprod().reindex(df.index[:end+1]).fillna(1)*df.A).ffill()


Output:



     A     B          C
0 10 0.07 10.000000
1 10 0.02 10.000000
2 10 0.05 10.000000
3 10 0.00 10.000000
4 10 0.01 10.000000
5 10 0.09 10.900000
6 10 0.00 10.900000
7 10 0.02 11.118000
8 10 0.03 11.451540
9 10 0.05 12.024117
10 10 0.05 12.625323
11 10 0.03 12.625323
12 10 0.01 12.625323
13 10 0.09 12.625323
14 10 0.06 12.625323
15 10 0.07 12.625323
16 10 0.01 12.625323
17 10 0.01 12.625323
18 10 0.01 12.625323
19 10 0.07 12.625323




Explanation:



Calculate the cumulative product of (1 + df.B), which is the factor to mulitply by df.A to obtain the recursive product. Do this only over the range specified. reindex and fill the the rows before start with 1, so the value remains constant before this range.



Multiply by df.A to get the actual value, forward filling values after the range you specify.






share|improve this answer



















  • 1




    This's exactly it! It works perfectly when using more than one range.
    – Arthur Coimbra
    Nov 18 '18 at 6:54














2












2








2






To get the recursive product you can do the following:



start = 5
end = 10

df['C'] = ((1+df.B)[start:end+1].cumprod().reindex(df.index[:end+1]).fillna(1)*df.A).ffill()


Output:



     A     B          C
0 10 0.07 10.000000
1 10 0.02 10.000000
2 10 0.05 10.000000
3 10 0.00 10.000000
4 10 0.01 10.000000
5 10 0.09 10.900000
6 10 0.00 10.900000
7 10 0.02 11.118000
8 10 0.03 11.451540
9 10 0.05 12.024117
10 10 0.05 12.625323
11 10 0.03 12.625323
12 10 0.01 12.625323
13 10 0.09 12.625323
14 10 0.06 12.625323
15 10 0.07 12.625323
16 10 0.01 12.625323
17 10 0.01 12.625323
18 10 0.01 12.625323
19 10 0.07 12.625323




Explanation:



Calculate the cumulative product of (1 + df.B), which is the factor to mulitply by df.A to obtain the recursive product. Do this only over the range specified. reindex and fill the the rows before start with 1, so the value remains constant before this range.



Multiply by df.A to get the actual value, forward filling values after the range you specify.






share|improve this answer














To get the recursive product you can do the following:



start = 5
end = 10

df['C'] = ((1+df.B)[start:end+1].cumprod().reindex(df.index[:end+1]).fillna(1)*df.A).ffill()


Output:



     A     B          C
0 10 0.07 10.000000
1 10 0.02 10.000000
2 10 0.05 10.000000
3 10 0.00 10.000000
4 10 0.01 10.000000
5 10 0.09 10.900000
6 10 0.00 10.900000
7 10 0.02 11.118000
8 10 0.03 11.451540
9 10 0.05 12.024117
10 10 0.05 12.625323
11 10 0.03 12.625323
12 10 0.01 12.625323
13 10 0.09 12.625323
14 10 0.06 12.625323
15 10 0.07 12.625323
16 10 0.01 12.625323
17 10 0.01 12.625323
18 10 0.01 12.625323
19 10 0.07 12.625323




Explanation:



Calculate the cumulative product of (1 + df.B), which is the factor to mulitply by df.A to obtain the recursive product. Do this only over the range specified. reindex and fill the the rows before start with 1, so the value remains constant before this range.



Multiply by df.A to get the actual value, forward filling values after the range you specify.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 18 '18 at 3:51

























answered Nov 18 '18 at 3:38









ALollz

11.2k31334




11.2k31334








  • 1




    This's exactly it! It works perfectly when using more than one range.
    – Arthur Coimbra
    Nov 18 '18 at 6:54














  • 1




    This's exactly it! It works perfectly when using more than one range.
    – Arthur Coimbra
    Nov 18 '18 at 6:54








1




1




This's exactly it! It works perfectly when using more than one range.
– Arthur Coimbra
Nov 18 '18 at 6:54




This's exactly it! It works perfectly when using more than one range.
– Arthur Coimbra
Nov 18 '18 at 6:54


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53357204%2fhow-to-do-a-calculation-only-at-some-rows-of-my-dataframe%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

How to send String Array data to Server using php in android

Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

Is anime1.com a legal site for watching anime?