Summing day data values
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
add a comment |
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
add a comment |
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
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
python python-3.x list csv sum
asked Nov 20 at 21:08
Armeija
154
154
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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
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
|
show 3 more comments
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
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
|
show 3 more comments
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
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
|
show 3 more comments
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
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
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
|
show 3 more comments
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
|
show 3 more comments
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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