Summing day data values












1














I need help on my program with how to add up each day values. So far it looks like this



import csv

def convert(data):
try:
return int(data)
except ValueError:
return 0

with open('MonthData1.csv', 'r') as file1:
read_file = csv.reader(file1, delimiter = ';')
delheader = next(read_file)
for line in read_file:
valuedata = max(0,sum([convert(i) for i in line[1:5]]))
print(line[0], valuedata)


At the moment it sums the 3 item values as it should:



1.5.2018 15:00 150
1.5.2018 20:00 95
2.5.2018 18:00 105
4.5.2018 17:00 78
4.5.2018 20:00 0
4.5.2018 20:00 9


But I want it to sum all the values of the same day, so it would be something like this:



1.5.2018 245
2.5.2018 105
4.5.2018 87


How could this be excecuted? I hope for help. Here is a pastebin from all the data used in this: https://pastebin.com/Tw4aYdPc

The base code is not mine originally, edited it a bit to match my needs.










share|improve this question



























    1














    I need help on my program with how to add up each day values. So far it looks like this



    import csv

    def convert(data):
    try:
    return int(data)
    except ValueError:
    return 0

    with open('MonthData1.csv', 'r') as file1:
    read_file = csv.reader(file1, delimiter = ';')
    delheader = next(read_file)
    for line in read_file:
    valuedata = max(0,sum([convert(i) for i in line[1:5]]))
    print(line[0], valuedata)


    At the moment it sums the 3 item values as it should:



    1.5.2018 15:00 150
    1.5.2018 20:00 95
    2.5.2018 18:00 105
    4.5.2018 17:00 78
    4.5.2018 20:00 0
    4.5.2018 20:00 9


    But I want it to sum all the values of the same day, so it would be something like this:



    1.5.2018 245
    2.5.2018 105
    4.5.2018 87


    How could this be excecuted? I hope for help. Here is a pastebin from all the data used in this: https://pastebin.com/Tw4aYdPc

    The base code is not mine originally, edited it a bit to match my needs.










    share|improve this question

























      1












      1








      1







      I need help on my program with how to add up each day values. So far it looks like this



      import csv

      def convert(data):
      try:
      return int(data)
      except ValueError:
      return 0

      with open('MonthData1.csv', 'r') as file1:
      read_file = csv.reader(file1, delimiter = ';')
      delheader = next(read_file)
      for line in read_file:
      valuedata = max(0,sum([convert(i) for i in line[1:5]]))
      print(line[0], valuedata)


      At the moment it sums the 3 item values as it should:



      1.5.2018 15:00 150
      1.5.2018 20:00 95
      2.5.2018 18:00 105
      4.5.2018 17:00 78
      4.5.2018 20:00 0
      4.5.2018 20:00 9


      But I want it to sum all the values of the same day, so it would be something like this:



      1.5.2018 245
      2.5.2018 105
      4.5.2018 87


      How could this be excecuted? I hope for help. Here is a pastebin from all the data used in this: https://pastebin.com/Tw4aYdPc

      The base code is not mine originally, edited it a bit to match my needs.










      share|improve this question













      I need help on my program with how to add up each day values. So far it looks like this



      import csv

      def convert(data):
      try:
      return int(data)
      except ValueError:
      return 0

      with open('MonthData1.csv', 'r') as file1:
      read_file = csv.reader(file1, delimiter = ';')
      delheader = next(read_file)
      for line in read_file:
      valuedata = max(0,sum([convert(i) for i in line[1:5]]))
      print(line[0], valuedata)


      At the moment it sums the 3 item values as it should:



      1.5.2018 15:00 150
      1.5.2018 20:00 95
      2.5.2018 18:00 105
      4.5.2018 17:00 78
      4.5.2018 20:00 0
      4.5.2018 20:00 9


      But I want it to sum all the values of the same day, so it would be something like this:



      1.5.2018 245
      2.5.2018 105
      4.5.2018 87


      How could this be excecuted? I hope for help. Here is a pastebin from all the data used in this: https://pastebin.com/Tw4aYdPc

      The base code is not mine originally, edited it a bit to match my needs.







      python python-3.x list csv sum






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 20 at 21:08









      Armeija

      154




      154
























          1 Answer
          1






          active

          oldest

          votes


















          1














          I used a defaultdict to sum your valuedatas and ordered it to print:



          import csv
          from collections import defaultdict, OrderedDict


          def convert(data):
          try:
          return int(data)
          except ValueError:
          return 0


          with open('MonthData1.csv', 'r') as file1:
          read_file = csv.reader(file1, delimiter=';')
          delheader = next(read_file)
          data = defaultdict(int)
          for line in read_file:
          valuedata = max(0, sum([convert(i) for i in line[1:5]]))
          data[line[0].split()[0]] += valuedata

          for key in OrderedDict(sorted(data.items())):
          print('{} {}'.format(key, data[key]))


          EDIT:
          To calculate cumulative values:



          import csv
          from collections import defaultdict, OrderedDict


          def convert(data):
          try:
          return int(data)
          except ValueError:
          return 0


          with open('MonthData1.csv', 'r') as file1:
          read_file = csv.reader(file1, delimiter=';')
          delheader = next(read_file)
          data = defaultdict(int)
          for line in read_file:
          valuedata = max(0, sum([convert(i) for i in line[1:5]]))
          data[line[0].split()[0]] += valuedata

          previous_values =
          for key, value in OrderedDict(sorted(data.items())).items():
          print('{} {}'.format(key, value + sum(previous_values)))
          previous_values.append(value)


          Results:




          1.5.2018 245



          2.5.2018 350



          4.5.2018 433







          share|improve this answer























          • Wow thank you very much. Is it possible to add that after it has printed sums of the days, it calculates the cumulative values? It would pretty much be like: 1.5.2018 245, 2.5.2018 350, 4.5.2018 437 This would be helpful for me to analyze the values
            – Armeija
            Nov 20 at 21:38












          • Do you mean to maintain the prints you already did?
            – Aurora Wang
            Nov 20 at 21:42










          • After each day it maintains the value and sums the next values to the current one
            – Armeija
            Nov 20 at 21:46










          • Check if my edit does what you want.
            – Aurora Wang
            Nov 20 at 21:57










          • Can you print first the normal sum, and after it has printed the normal values it would print the cumulative values, something like this:1.5.2018 245 2.5.2018 105 4.5.2018 87 Then straight after it prints these 1.5.2018 245, 2.5.2018 350, 4.5.2018 437
            – Armeija
            Nov 20 at 22:03













          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%2f53401557%2fsumming-day-data-values%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









          1














          I used a defaultdict to sum your valuedatas and ordered it to print:



          import csv
          from collections import defaultdict, OrderedDict


          def convert(data):
          try:
          return int(data)
          except ValueError:
          return 0


          with open('MonthData1.csv', 'r') as file1:
          read_file = csv.reader(file1, delimiter=';')
          delheader = next(read_file)
          data = defaultdict(int)
          for line in read_file:
          valuedata = max(0, sum([convert(i) for i in line[1:5]]))
          data[line[0].split()[0]] += valuedata

          for key in OrderedDict(sorted(data.items())):
          print('{} {}'.format(key, data[key]))


          EDIT:
          To calculate cumulative values:



          import csv
          from collections import defaultdict, OrderedDict


          def convert(data):
          try:
          return int(data)
          except ValueError:
          return 0


          with open('MonthData1.csv', 'r') as file1:
          read_file = csv.reader(file1, delimiter=';')
          delheader = next(read_file)
          data = defaultdict(int)
          for line in read_file:
          valuedata = max(0, sum([convert(i) for i in line[1:5]]))
          data[line[0].split()[0]] += valuedata

          previous_values =
          for key, value in OrderedDict(sorted(data.items())).items():
          print('{} {}'.format(key, value + sum(previous_values)))
          previous_values.append(value)


          Results:




          1.5.2018 245



          2.5.2018 350



          4.5.2018 433







          share|improve this answer























          • Wow thank you very much. Is it possible to add that after it has printed sums of the days, it calculates the cumulative values? It would pretty much be like: 1.5.2018 245, 2.5.2018 350, 4.5.2018 437 This would be helpful for me to analyze the values
            – Armeija
            Nov 20 at 21:38












          • Do you mean to maintain the prints you already did?
            – Aurora Wang
            Nov 20 at 21:42










          • After each day it maintains the value and sums the next values to the current one
            – Armeija
            Nov 20 at 21:46










          • Check if my edit does what you want.
            – Aurora Wang
            Nov 20 at 21:57










          • Can you print first the normal sum, and after it has printed the normal values it would print the cumulative values, something like this:1.5.2018 245 2.5.2018 105 4.5.2018 87 Then straight after it prints these 1.5.2018 245, 2.5.2018 350, 4.5.2018 437
            – Armeija
            Nov 20 at 22:03


















          1














          I used a defaultdict to sum your valuedatas and ordered it to print:



          import csv
          from collections import defaultdict, OrderedDict


          def convert(data):
          try:
          return int(data)
          except ValueError:
          return 0


          with open('MonthData1.csv', 'r') as file1:
          read_file = csv.reader(file1, delimiter=';')
          delheader = next(read_file)
          data = defaultdict(int)
          for line in read_file:
          valuedata = max(0, sum([convert(i) for i in line[1:5]]))
          data[line[0].split()[0]] += valuedata

          for key in OrderedDict(sorted(data.items())):
          print('{} {}'.format(key, data[key]))


          EDIT:
          To calculate cumulative values:



          import csv
          from collections import defaultdict, OrderedDict


          def convert(data):
          try:
          return int(data)
          except ValueError:
          return 0


          with open('MonthData1.csv', 'r') as file1:
          read_file = csv.reader(file1, delimiter=';')
          delheader = next(read_file)
          data = defaultdict(int)
          for line in read_file:
          valuedata = max(0, sum([convert(i) for i in line[1:5]]))
          data[line[0].split()[0]] += valuedata

          previous_values =
          for key, value in OrderedDict(sorted(data.items())).items():
          print('{} {}'.format(key, value + sum(previous_values)))
          previous_values.append(value)


          Results:




          1.5.2018 245



          2.5.2018 350



          4.5.2018 433







          share|improve this answer























          • Wow thank you very much. Is it possible to add that after it has printed sums of the days, it calculates the cumulative values? It would pretty much be like: 1.5.2018 245, 2.5.2018 350, 4.5.2018 437 This would be helpful for me to analyze the values
            – Armeija
            Nov 20 at 21:38












          • Do you mean to maintain the prints you already did?
            – Aurora Wang
            Nov 20 at 21:42










          • After each day it maintains the value and sums the next values to the current one
            – Armeija
            Nov 20 at 21:46










          • Check if my edit does what you want.
            – Aurora Wang
            Nov 20 at 21:57










          • Can you print first the normal sum, and after it has printed the normal values it would print the cumulative values, something like this:1.5.2018 245 2.5.2018 105 4.5.2018 87 Then straight after it prints these 1.5.2018 245, 2.5.2018 350, 4.5.2018 437
            – Armeija
            Nov 20 at 22:03
















          1












          1








          1






          I used a defaultdict to sum your valuedatas and ordered it to print:



          import csv
          from collections import defaultdict, OrderedDict


          def convert(data):
          try:
          return int(data)
          except ValueError:
          return 0


          with open('MonthData1.csv', 'r') as file1:
          read_file = csv.reader(file1, delimiter=';')
          delheader = next(read_file)
          data = defaultdict(int)
          for line in read_file:
          valuedata = max(0, sum([convert(i) for i in line[1:5]]))
          data[line[0].split()[0]] += valuedata

          for key in OrderedDict(sorted(data.items())):
          print('{} {}'.format(key, data[key]))


          EDIT:
          To calculate cumulative values:



          import csv
          from collections import defaultdict, OrderedDict


          def convert(data):
          try:
          return int(data)
          except ValueError:
          return 0


          with open('MonthData1.csv', 'r') as file1:
          read_file = csv.reader(file1, delimiter=';')
          delheader = next(read_file)
          data = defaultdict(int)
          for line in read_file:
          valuedata = max(0, sum([convert(i) for i in line[1:5]]))
          data[line[0].split()[0]] += valuedata

          previous_values =
          for key, value in OrderedDict(sorted(data.items())).items():
          print('{} {}'.format(key, value + sum(previous_values)))
          previous_values.append(value)


          Results:




          1.5.2018 245



          2.5.2018 350



          4.5.2018 433







          share|improve this answer














          I used a defaultdict to sum your valuedatas and ordered it to print:



          import csv
          from collections import defaultdict, OrderedDict


          def convert(data):
          try:
          return int(data)
          except ValueError:
          return 0


          with open('MonthData1.csv', 'r') as file1:
          read_file = csv.reader(file1, delimiter=';')
          delheader = next(read_file)
          data = defaultdict(int)
          for line in read_file:
          valuedata = max(0, sum([convert(i) for i in line[1:5]]))
          data[line[0].split()[0]] += valuedata

          for key in OrderedDict(sorted(data.items())):
          print('{} {}'.format(key, data[key]))


          EDIT:
          To calculate cumulative values:



          import csv
          from collections import defaultdict, OrderedDict


          def convert(data):
          try:
          return int(data)
          except ValueError:
          return 0


          with open('MonthData1.csv', 'r') as file1:
          read_file = csv.reader(file1, delimiter=';')
          delheader = next(read_file)
          data = defaultdict(int)
          for line in read_file:
          valuedata = max(0, sum([convert(i) for i in line[1:5]]))
          data[line[0].split()[0]] += valuedata

          previous_values =
          for key, value in OrderedDict(sorted(data.items())).items():
          print('{} {}'.format(key, value + sum(previous_values)))
          previous_values.append(value)


          Results:




          1.5.2018 245



          2.5.2018 350



          4.5.2018 433








          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 20 at 21:57

























          answered Nov 20 at 21:26









          Aurora Wang

          682216




          682216












          • Wow thank you very much. Is it possible to add that after it has printed sums of the days, it calculates the cumulative values? It would pretty much be like: 1.5.2018 245, 2.5.2018 350, 4.5.2018 437 This would be helpful for me to analyze the values
            – Armeija
            Nov 20 at 21:38












          • Do you mean to maintain the prints you already did?
            – Aurora Wang
            Nov 20 at 21:42










          • After each day it maintains the value and sums the next values to the current one
            – Armeija
            Nov 20 at 21:46










          • Check if my edit does what you want.
            – Aurora Wang
            Nov 20 at 21:57










          • Can you print first the normal sum, and after it has printed the normal values it would print the cumulative values, something like this:1.5.2018 245 2.5.2018 105 4.5.2018 87 Then straight after it prints these 1.5.2018 245, 2.5.2018 350, 4.5.2018 437
            – Armeija
            Nov 20 at 22:03




















          • Wow thank you very much. Is it possible to add that after it has printed sums of the days, it calculates the cumulative values? It would pretty much be like: 1.5.2018 245, 2.5.2018 350, 4.5.2018 437 This would be helpful for me to analyze the values
            – Armeija
            Nov 20 at 21:38












          • Do you mean to maintain the prints you already did?
            – Aurora Wang
            Nov 20 at 21:42










          • After each day it maintains the value and sums the next values to the current one
            – Armeija
            Nov 20 at 21:46










          • Check if my edit does what you want.
            – Aurora Wang
            Nov 20 at 21:57










          • Can you print first the normal sum, and after it has printed the normal values it would print the cumulative values, something like this:1.5.2018 245 2.5.2018 105 4.5.2018 87 Then straight after it prints these 1.5.2018 245, 2.5.2018 350, 4.5.2018 437
            – Armeija
            Nov 20 at 22:03


















          Wow thank you very much. Is it possible to add that after it has printed sums of the days, it calculates the cumulative values? It would pretty much be like: 1.5.2018 245, 2.5.2018 350, 4.5.2018 437 This would be helpful for me to analyze the values
          – Armeija
          Nov 20 at 21:38






          Wow thank you very much. Is it possible to add that after it has printed sums of the days, it calculates the cumulative values? It would pretty much be like: 1.5.2018 245, 2.5.2018 350, 4.5.2018 437 This would be helpful for me to analyze the values
          – Armeija
          Nov 20 at 21:38














          Do you mean to maintain the prints you already did?
          – Aurora Wang
          Nov 20 at 21:42




          Do you mean to maintain the prints you already did?
          – Aurora Wang
          Nov 20 at 21:42












          After each day it maintains the value and sums the next values to the current one
          – Armeija
          Nov 20 at 21:46




          After each day it maintains the value and sums the next values to the current one
          – Armeija
          Nov 20 at 21:46












          Check if my edit does what you want.
          – Aurora Wang
          Nov 20 at 21:57




          Check if my edit does what you want.
          – Aurora Wang
          Nov 20 at 21:57












          Can you print first the normal sum, and after it has printed the normal values it would print the cumulative values, something like this:1.5.2018 245 2.5.2018 105 4.5.2018 87 Then straight after it prints these 1.5.2018 245, 2.5.2018 350, 4.5.2018 437
          – Armeija
          Nov 20 at 22:03






          Can you print first the normal sum, and after it has printed the normal values it would print the cumulative values, something like this:1.5.2018 245 2.5.2018 105 4.5.2018 87 Then straight after it prints these 1.5.2018 245, 2.5.2018 350, 4.5.2018 437
          – Armeija
          Nov 20 at 22:03




















          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%2f53401557%2fsumming-day-data-values%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

          Wiesbaden

          Marschland

          Dieringhausen