Python - determine change in value from one period to the next












-3















Thanks in advance for your help. I am new to Python (and coding in general), and am trying to do several things, with the imported CSV file below. The problem that I am having is capturing (in a list) the change in revenue from one row to the next for example, the change in revenue from 10/18 to 11/18. Ideally, I would have a final list with ["month", "change vs previous"].



Here is my existing code:



import os

import csv

csvpath = os.path.join("C:/test2", "budget_data.csv")

num_rows=0
total_rev=0

with open(csvpath, newline="") as csvfile:
csvreader = csv.reader(csvfile, delimiter= ",")
print(csvreader)
csv_header =next(csvreader)
print(f"CSV Header: {csv_header}")
for row in csvreader:
num_rows=num_rows+1
total_rev += int(row[1])


print("")
print("There are "+ str(num_rows) +" months of data!")
print("Total Revenue is "+str(total_rev)+" dollars!")









share|improve this question

























  • Can you please share a sample of your csv file?

    – Mayank Porwal
    Nov 26 '18 at 7:19











  • Thank you for responding. I have attached some anonymized data. It is not the actual data, but is in the exact same format, including the headers.

    – Earl
    Nov 26 '18 at 13:52













  • Date Profit/Losses Jan-10 867884 Feb-10 984655 Mar-10 322013 Apr-10 -69417 May-10 310503 Jun-10 522857 Jul-10 1033096 Aug-10 604885 Sep-10 -216386 Oct-10 477532 Nov-10 893810 Dec-10 -80353 Jan-11 779806 Feb-11 -335203 Mar-11 697845 Apr-11 793163 May-11 485070 Jun-11 584122 Jul-11 62729 Aug-11 668179 Sep-11 899906 Oct-11 834719 Nov-11 132003 Dec-11 309978 Jan-12 -755566 Feb-12 1170593 Mar-12 252788 Apr-12 1151518 May-12 817256 Jun-12 570757 Jul-12 506702 Aug-12 -1022534 Sep-12 475062 Oct-12 779976 Nov-12 144175 Dec-12 542494 Jan-13 359333

    – Earl
    Nov 26 '18 at 13:54











  • You can use the edit button to add additional information to your question.

    – Martin Evans
    Nov 27 '18 at 7:17
















-3















Thanks in advance for your help. I am new to Python (and coding in general), and am trying to do several things, with the imported CSV file below. The problem that I am having is capturing (in a list) the change in revenue from one row to the next for example, the change in revenue from 10/18 to 11/18. Ideally, I would have a final list with ["month", "change vs previous"].



Here is my existing code:



import os

import csv

csvpath = os.path.join("C:/test2", "budget_data.csv")

num_rows=0
total_rev=0

with open(csvpath, newline="") as csvfile:
csvreader = csv.reader(csvfile, delimiter= ",")
print(csvreader)
csv_header =next(csvreader)
print(f"CSV Header: {csv_header}")
for row in csvreader:
num_rows=num_rows+1
total_rev += int(row[1])


print("")
print("There are "+ str(num_rows) +" months of data!")
print("Total Revenue is "+str(total_rev)+" dollars!")









share|improve this question

























  • Can you please share a sample of your csv file?

    – Mayank Porwal
    Nov 26 '18 at 7:19











  • Thank you for responding. I have attached some anonymized data. It is not the actual data, but is in the exact same format, including the headers.

    – Earl
    Nov 26 '18 at 13:52













  • Date Profit/Losses Jan-10 867884 Feb-10 984655 Mar-10 322013 Apr-10 -69417 May-10 310503 Jun-10 522857 Jul-10 1033096 Aug-10 604885 Sep-10 -216386 Oct-10 477532 Nov-10 893810 Dec-10 -80353 Jan-11 779806 Feb-11 -335203 Mar-11 697845 Apr-11 793163 May-11 485070 Jun-11 584122 Jul-11 62729 Aug-11 668179 Sep-11 899906 Oct-11 834719 Nov-11 132003 Dec-11 309978 Jan-12 -755566 Feb-12 1170593 Mar-12 252788 Apr-12 1151518 May-12 817256 Jun-12 570757 Jul-12 506702 Aug-12 -1022534 Sep-12 475062 Oct-12 779976 Nov-12 144175 Dec-12 542494 Jan-13 359333

    – Earl
    Nov 26 '18 at 13:54











  • You can use the edit button to add additional information to your question.

    – Martin Evans
    Nov 27 '18 at 7:17














-3












-3








-3








Thanks in advance for your help. I am new to Python (and coding in general), and am trying to do several things, with the imported CSV file below. The problem that I am having is capturing (in a list) the change in revenue from one row to the next for example, the change in revenue from 10/18 to 11/18. Ideally, I would have a final list with ["month", "change vs previous"].



Here is my existing code:



import os

import csv

csvpath = os.path.join("C:/test2", "budget_data.csv")

num_rows=0
total_rev=0

with open(csvpath, newline="") as csvfile:
csvreader = csv.reader(csvfile, delimiter= ",")
print(csvreader)
csv_header =next(csvreader)
print(f"CSV Header: {csv_header}")
for row in csvreader:
num_rows=num_rows+1
total_rev += int(row[1])


print("")
print("There are "+ str(num_rows) +" months of data!")
print("Total Revenue is "+str(total_rev)+" dollars!")









share|improve this question
















Thanks in advance for your help. I am new to Python (and coding in general), and am trying to do several things, with the imported CSV file below. The problem that I am having is capturing (in a list) the change in revenue from one row to the next for example, the change in revenue from 10/18 to 11/18. Ideally, I would have a final list with ["month", "change vs previous"].



Here is my existing code:



import os

import csv

csvpath = os.path.join("C:/test2", "budget_data.csv")

num_rows=0
total_rev=0

with open(csvpath, newline="") as csvfile:
csvreader = csv.reader(csvfile, delimiter= ",")
print(csvreader)
csv_header =next(csvreader)
print(f"CSV Header: {csv_header}")
for row in csvreader:
num_rows=num_rows+1
total_rev += int(row[1])


print("")
print("There are "+ str(num_rows) +" months of data!")
print("Total Revenue is "+str(total_rev)+" dollars!")






python list csv import






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 26 '18 at 3:33









Ryan Lee

645




645










asked Nov 26 '18 at 2:39









EarlEarl

11




11













  • Can you please share a sample of your csv file?

    – Mayank Porwal
    Nov 26 '18 at 7:19











  • Thank you for responding. I have attached some anonymized data. It is not the actual data, but is in the exact same format, including the headers.

    – Earl
    Nov 26 '18 at 13:52













  • Date Profit/Losses Jan-10 867884 Feb-10 984655 Mar-10 322013 Apr-10 -69417 May-10 310503 Jun-10 522857 Jul-10 1033096 Aug-10 604885 Sep-10 -216386 Oct-10 477532 Nov-10 893810 Dec-10 -80353 Jan-11 779806 Feb-11 -335203 Mar-11 697845 Apr-11 793163 May-11 485070 Jun-11 584122 Jul-11 62729 Aug-11 668179 Sep-11 899906 Oct-11 834719 Nov-11 132003 Dec-11 309978 Jan-12 -755566 Feb-12 1170593 Mar-12 252788 Apr-12 1151518 May-12 817256 Jun-12 570757 Jul-12 506702 Aug-12 -1022534 Sep-12 475062 Oct-12 779976 Nov-12 144175 Dec-12 542494 Jan-13 359333

    – Earl
    Nov 26 '18 at 13:54











  • You can use the edit button to add additional information to your question.

    – Martin Evans
    Nov 27 '18 at 7:17



















  • Can you please share a sample of your csv file?

    – Mayank Porwal
    Nov 26 '18 at 7:19











  • Thank you for responding. I have attached some anonymized data. It is not the actual data, but is in the exact same format, including the headers.

    – Earl
    Nov 26 '18 at 13:52













  • Date Profit/Losses Jan-10 867884 Feb-10 984655 Mar-10 322013 Apr-10 -69417 May-10 310503 Jun-10 522857 Jul-10 1033096 Aug-10 604885 Sep-10 -216386 Oct-10 477532 Nov-10 893810 Dec-10 -80353 Jan-11 779806 Feb-11 -335203 Mar-11 697845 Apr-11 793163 May-11 485070 Jun-11 584122 Jul-11 62729 Aug-11 668179 Sep-11 899906 Oct-11 834719 Nov-11 132003 Dec-11 309978 Jan-12 -755566 Feb-12 1170593 Mar-12 252788 Apr-12 1151518 May-12 817256 Jun-12 570757 Jul-12 506702 Aug-12 -1022534 Sep-12 475062 Oct-12 779976 Nov-12 144175 Dec-12 542494 Jan-13 359333

    – Earl
    Nov 26 '18 at 13:54











  • You can use the edit button to add additional information to your question.

    – Martin Evans
    Nov 27 '18 at 7:17

















Can you please share a sample of your csv file?

– Mayank Porwal
Nov 26 '18 at 7:19





Can you please share a sample of your csv file?

– Mayank Porwal
Nov 26 '18 at 7:19













Thank you for responding. I have attached some anonymized data. It is not the actual data, but is in the exact same format, including the headers.

– Earl
Nov 26 '18 at 13:52







Thank you for responding. I have attached some anonymized data. It is not the actual data, but is in the exact same format, including the headers.

– Earl
Nov 26 '18 at 13:52















Date Profit/Losses Jan-10 867884 Feb-10 984655 Mar-10 322013 Apr-10 -69417 May-10 310503 Jun-10 522857 Jul-10 1033096 Aug-10 604885 Sep-10 -216386 Oct-10 477532 Nov-10 893810 Dec-10 -80353 Jan-11 779806 Feb-11 -335203 Mar-11 697845 Apr-11 793163 May-11 485070 Jun-11 584122 Jul-11 62729 Aug-11 668179 Sep-11 899906 Oct-11 834719 Nov-11 132003 Dec-11 309978 Jan-12 -755566 Feb-12 1170593 Mar-12 252788 Apr-12 1151518 May-12 817256 Jun-12 570757 Jul-12 506702 Aug-12 -1022534 Sep-12 475062 Oct-12 779976 Nov-12 144175 Dec-12 542494 Jan-13 359333

– Earl
Nov 26 '18 at 13:54





Date Profit/Losses Jan-10 867884 Feb-10 984655 Mar-10 322013 Apr-10 -69417 May-10 310503 Jun-10 522857 Jul-10 1033096 Aug-10 604885 Sep-10 -216386 Oct-10 477532 Nov-10 893810 Dec-10 -80353 Jan-11 779806 Feb-11 -335203 Mar-11 697845 Apr-11 793163 May-11 485070 Jun-11 584122 Jul-11 62729 Aug-11 668179 Sep-11 899906 Oct-11 834719 Nov-11 132003 Dec-11 309978 Jan-12 -755566 Feb-12 1170593 Mar-12 252788 Apr-12 1151518 May-12 817256 Jun-12 570757 Jul-12 506702 Aug-12 -1022534 Sep-12 475062 Oct-12 779976 Nov-12 144175 Dec-12 542494 Jan-13 359333

– Earl
Nov 26 '18 at 13:54













You can use the edit button to add additional information to your question.

– Martin Evans
Nov 27 '18 at 7:17





You can use the edit button to add additional information to your question.

– Martin Evans
Nov 27 '18 at 7:17












1 Answer
1






active

oldest

votes


















0














You need to add a variable to hold the revenue for the previous row. This is updated at the end of each loop:



import os
import csv

csvpath = os.path.join("C:/test2", "budget_data.csv")

num_rows = 0
total_rev = 0
change_from_previous =

with open(csvpath, newline="") as csvfile:
csvreader = csv.reader(csvfile, delimiter=",")
print(csvreader)
csv_header = next(csvreader)
print(f"CSV Header: {csv_header}")
previous_revenue = 0

for row in csvreader:
revenue = int(row[1])
num_rows += 1
total_rev += revenue
change_from_previous.append([row[0], revenue - previous_revenue])
previous_revenue = revenue

print("")
print("There are "+ str(num_rows) +" months of data!")
print("Total Revenue is "+str(total_rev)+" dollars!")
print(change_from_previous)


This would display a list holding each month and change:



[['Jan-10', 867884], ['Feb-10', 116771], ['Mar-10', -662642], ['Apr-10', -391430], ['May-10', 379920], ['Jun-10', 212354], ['Jul-10', 510239], ['Aug-10', -428211], ['Sep-10', -821271], ['Oct-10', 693918], ['Nov-10', 416278], ['Dec-10', -974163], ['Jan-11', 860159], ['Feb-11', -1115009], ['Mar-11', 1033048], ['Apr-11', 95318], ['May-11', -308093], ['Jun-11', 99052], ['Jul-11', -521393], ['Aug-11', 605450], ['Sep-11', 231727], ['Oct-11', -65187], ['Nov-11', -702716], ['Dec-11', 177975], ['Jan-12', -1065544], ['Feb-12', 1926159], ['Mar-12', -917805], ['Apr-12', 898730], ['May-12', -334262], ['Jun-12', -246499], ['Jul-12', -64055], ['Aug-12', -1529236], ['Sep-12', 1497596], ['Oct-12', 304914], ['Nov-12', -635801], ['Dec-12', 398319], ['Jan-13', -183161]]





share|improve this answer























    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%2f53474110%2fpython-determine-change-in-value-from-one-period-to-the-next%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









    0














    You need to add a variable to hold the revenue for the previous row. This is updated at the end of each loop:



    import os
    import csv

    csvpath = os.path.join("C:/test2", "budget_data.csv")

    num_rows = 0
    total_rev = 0
    change_from_previous =

    with open(csvpath, newline="") as csvfile:
    csvreader = csv.reader(csvfile, delimiter=",")
    print(csvreader)
    csv_header = next(csvreader)
    print(f"CSV Header: {csv_header}")
    previous_revenue = 0

    for row in csvreader:
    revenue = int(row[1])
    num_rows += 1
    total_rev += revenue
    change_from_previous.append([row[0], revenue - previous_revenue])
    previous_revenue = revenue

    print("")
    print("There are "+ str(num_rows) +" months of data!")
    print("Total Revenue is "+str(total_rev)+" dollars!")
    print(change_from_previous)


    This would display a list holding each month and change:



    [['Jan-10', 867884], ['Feb-10', 116771], ['Mar-10', -662642], ['Apr-10', -391430], ['May-10', 379920], ['Jun-10', 212354], ['Jul-10', 510239], ['Aug-10', -428211], ['Sep-10', -821271], ['Oct-10', 693918], ['Nov-10', 416278], ['Dec-10', -974163], ['Jan-11', 860159], ['Feb-11', -1115009], ['Mar-11', 1033048], ['Apr-11', 95318], ['May-11', -308093], ['Jun-11', 99052], ['Jul-11', -521393], ['Aug-11', 605450], ['Sep-11', 231727], ['Oct-11', -65187], ['Nov-11', -702716], ['Dec-11', 177975], ['Jan-12', -1065544], ['Feb-12', 1926159], ['Mar-12', -917805], ['Apr-12', 898730], ['May-12', -334262], ['Jun-12', -246499], ['Jul-12', -64055], ['Aug-12', -1529236], ['Sep-12', 1497596], ['Oct-12', 304914], ['Nov-12', -635801], ['Dec-12', 398319], ['Jan-13', -183161]]





    share|improve this answer




























      0














      You need to add a variable to hold the revenue for the previous row. This is updated at the end of each loop:



      import os
      import csv

      csvpath = os.path.join("C:/test2", "budget_data.csv")

      num_rows = 0
      total_rev = 0
      change_from_previous =

      with open(csvpath, newline="") as csvfile:
      csvreader = csv.reader(csvfile, delimiter=",")
      print(csvreader)
      csv_header = next(csvreader)
      print(f"CSV Header: {csv_header}")
      previous_revenue = 0

      for row in csvreader:
      revenue = int(row[1])
      num_rows += 1
      total_rev += revenue
      change_from_previous.append([row[0], revenue - previous_revenue])
      previous_revenue = revenue

      print("")
      print("There are "+ str(num_rows) +" months of data!")
      print("Total Revenue is "+str(total_rev)+" dollars!")
      print(change_from_previous)


      This would display a list holding each month and change:



      [['Jan-10', 867884], ['Feb-10', 116771], ['Mar-10', -662642], ['Apr-10', -391430], ['May-10', 379920], ['Jun-10', 212354], ['Jul-10', 510239], ['Aug-10', -428211], ['Sep-10', -821271], ['Oct-10', 693918], ['Nov-10', 416278], ['Dec-10', -974163], ['Jan-11', 860159], ['Feb-11', -1115009], ['Mar-11', 1033048], ['Apr-11', 95318], ['May-11', -308093], ['Jun-11', 99052], ['Jul-11', -521393], ['Aug-11', 605450], ['Sep-11', 231727], ['Oct-11', -65187], ['Nov-11', -702716], ['Dec-11', 177975], ['Jan-12', -1065544], ['Feb-12', 1926159], ['Mar-12', -917805], ['Apr-12', 898730], ['May-12', -334262], ['Jun-12', -246499], ['Jul-12', -64055], ['Aug-12', -1529236], ['Sep-12', 1497596], ['Oct-12', 304914], ['Nov-12', -635801], ['Dec-12', 398319], ['Jan-13', -183161]]





      share|improve this answer


























        0












        0








        0







        You need to add a variable to hold the revenue for the previous row. This is updated at the end of each loop:



        import os
        import csv

        csvpath = os.path.join("C:/test2", "budget_data.csv")

        num_rows = 0
        total_rev = 0
        change_from_previous =

        with open(csvpath, newline="") as csvfile:
        csvreader = csv.reader(csvfile, delimiter=",")
        print(csvreader)
        csv_header = next(csvreader)
        print(f"CSV Header: {csv_header}")
        previous_revenue = 0

        for row in csvreader:
        revenue = int(row[1])
        num_rows += 1
        total_rev += revenue
        change_from_previous.append([row[0], revenue - previous_revenue])
        previous_revenue = revenue

        print("")
        print("There are "+ str(num_rows) +" months of data!")
        print("Total Revenue is "+str(total_rev)+" dollars!")
        print(change_from_previous)


        This would display a list holding each month and change:



        [['Jan-10', 867884], ['Feb-10', 116771], ['Mar-10', -662642], ['Apr-10', -391430], ['May-10', 379920], ['Jun-10', 212354], ['Jul-10', 510239], ['Aug-10', -428211], ['Sep-10', -821271], ['Oct-10', 693918], ['Nov-10', 416278], ['Dec-10', -974163], ['Jan-11', 860159], ['Feb-11', -1115009], ['Mar-11', 1033048], ['Apr-11', 95318], ['May-11', -308093], ['Jun-11', 99052], ['Jul-11', -521393], ['Aug-11', 605450], ['Sep-11', 231727], ['Oct-11', -65187], ['Nov-11', -702716], ['Dec-11', 177975], ['Jan-12', -1065544], ['Feb-12', 1926159], ['Mar-12', -917805], ['Apr-12', 898730], ['May-12', -334262], ['Jun-12', -246499], ['Jul-12', -64055], ['Aug-12', -1529236], ['Sep-12', 1497596], ['Oct-12', 304914], ['Nov-12', -635801], ['Dec-12', 398319], ['Jan-13', -183161]]





        share|improve this answer













        You need to add a variable to hold the revenue for the previous row. This is updated at the end of each loop:



        import os
        import csv

        csvpath = os.path.join("C:/test2", "budget_data.csv")

        num_rows = 0
        total_rev = 0
        change_from_previous =

        with open(csvpath, newline="") as csvfile:
        csvreader = csv.reader(csvfile, delimiter=",")
        print(csvreader)
        csv_header = next(csvreader)
        print(f"CSV Header: {csv_header}")
        previous_revenue = 0

        for row in csvreader:
        revenue = int(row[1])
        num_rows += 1
        total_rev += revenue
        change_from_previous.append([row[0], revenue - previous_revenue])
        previous_revenue = revenue

        print("")
        print("There are "+ str(num_rows) +" months of data!")
        print("Total Revenue is "+str(total_rev)+" dollars!")
        print(change_from_previous)


        This would display a list holding each month and change:



        [['Jan-10', 867884], ['Feb-10', 116771], ['Mar-10', -662642], ['Apr-10', -391430], ['May-10', 379920], ['Jun-10', 212354], ['Jul-10', 510239], ['Aug-10', -428211], ['Sep-10', -821271], ['Oct-10', 693918], ['Nov-10', 416278], ['Dec-10', -974163], ['Jan-11', 860159], ['Feb-11', -1115009], ['Mar-11', 1033048], ['Apr-11', 95318], ['May-11', -308093], ['Jun-11', 99052], ['Jul-11', -521393], ['Aug-11', 605450], ['Sep-11', 231727], ['Oct-11', -65187], ['Nov-11', -702716], ['Dec-11', 177975], ['Jan-12', -1065544], ['Feb-12', 1926159], ['Mar-12', -917805], ['Apr-12', 898730], ['May-12', -334262], ['Jun-12', -246499], ['Jul-12', -64055], ['Aug-12', -1529236], ['Sep-12', 1497596], ['Oct-12', 304914], ['Nov-12', -635801], ['Dec-12', 398319], ['Jan-13', -183161]]






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 27 '18 at 7:45









        Martin EvansMartin Evans

        28.3k133156




        28.3k133156
































            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.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53474110%2fpython-determine-change-in-value-from-one-period-to-the-next%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

            404 Error Contact Form 7 ajax form submitting

            How to know if a Active Directory user can login interactively

            TypeError: fit_transform() missing 1 required positional argument: 'X'