Help with calcuations using imported numbers. 
July 6th, 2009, 07:56 PM
| | Newbie | | Join Date: Jul 2009
Posts: 22
| |
I have imported two columns of numbers from excel and now i need to use those two columns of numbers to calculate the following...
Average_Error = sum(map(lambda a, b: abs(a - b), My_High, Actual_High))/delta.days
I need the first column to be inserted where I have My_High in the code above, and I need the second column to be inserted where I have Actual_High in the code above. I used the following code to import the numbers... -
f = open("C:\users\cory\desktop\Verification.csv")
-
dd = {}
-
keys = f.readline().strip().split(',')
-
for key in keys:
-
dd.setdefault(key, [])
-
-
for line in f:
-
for i, item in enumerate(line.strip().split(',')):
-
dd[keys[i]].append(int(item))
-
-
f.close()
I don't know where to begin.
Thanks!
Last edited by bvdet; July 6th, 2009 at 08:47 PM.
Reason: Added code tags
| 
July 6th, 2009, 09:00 PM
|  | Moderator | | Join Date: Oct 2006 Location: Nashville, TN
Posts: 1,545
| | | re: Help with calcuations using imported numbers.
Please use code tags when posting code. See this link.
What is delta.days?
Access the list My_High like this: | 
July 6th, 2009, 09:30 PM
| | Newbie | | Join Date: Jul 2009
Posts: 22
| | | re: Help with calcuations using imported numbers.
delta.days is just the number of days in between two dates... -
-
-
today = datetime.date.today()
-
date_format = "%m/%d/%Y"
-
d0 = date(2009, 6, 21)
-
d1 = today
-
delta = d1-d0
-
print delta.days
-
I have this as my code now... -
-
f = open("C:\users\cory\desktop\Verification.csv")
-
dd = ['My_Highs']
-
keys = f.readline().strip().split(',')
-
for key in keys:
-
dd.setdefault(key, ['My_Highs'])
-
-
for line in f:
-
for i, item in enumerate(line.strip().split(',')):
-
dd[keys[i]].append(int(item))
-
-
f.close()
-
-
...and I am getting this error message...Attribute Error: 'list' object has no attribute 'setdefault'
| 
July 6th, 2009, 09:51 PM
|  | Moderator | | Join Date: Oct 2006 Location: Nashville, TN
Posts: 1,545
| | | re: Help with calcuations using imported numbers.
Evidently you did not look at the code to understand what is happening. I will attempt to explain with embedded comments: - # Create an open file object for reading.
-
f = open('your_file.csv')
-
# Initialize an empty dictionary.
-
dd = {}
-
# Read the first line in the file.
-
# These are the column labels.
-
# We will use these labels as the dictionary keys.
-
# Create a list by splitting the string on the comma.
-
keys = f.readline().strip().split(',')
-
# Iterate on the list of labels.
-
for key in keys:
-
# Using dictionary method setdefault, assign each dictionary
-
# key to an empty list.
-
dd.setdefault(key, [])
-
-
# Iterate on the file object.
-
# This iteration begins with line 2 because we already read the first line.
-
for line in f:
-
# Create a list of scores out of each line by splitting the string
-
# on the comma character. Iterate on the list of scores.
-
for i, item in enumerate(line.strip().split(',')):
-
# Append each score to the value of the corresponding
-
# dictionary key. Type cast the score to int.
-
dd[keys[i]].append(int(item))
-
-
# Close the file object.
-
f.close()
-
-
# Iterate on the list of keys (labels).
-
for key in keys:
-
# Print the key and corresponding dictionary value.
-
print "%s: %s" % (key, dd[key])
Study the comments above and post your question again.
| 
July 6th, 2009, 10:15 PM
| | Newbie | | Join Date: Jul 2009
Posts: 22
| | | re: Help with calcuations using imported numbers.
The comments helped a lot, I figured it out! Thanks for all your help.
| 
July 6th, 2009, 10:45 PM
|  | Moderator | | Join Date: Oct 2006 Location: Nashville, TN
Posts: 1,545
| | | re: Help with calcuations using imported numbers.
You are welcome. I think you learned something.
-BV
| 
July 6th, 2009, 10:59 PM
| | Newbie | | Join Date: Jul 2009
Posts: 22
| | | re: Help with calcuations using imported numbers.
I did! This is only my third day working with python, and I have never written code before so I will more than likely have some more questions with due time.
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 225,702 network members.
|