Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

College assigment question

Options
  • 26-10-2015 9:38pm
    #1
    Closed Accounts Posts: 4,935 ✭✭✭


    I have a college assignment, I have to open a CSV file in Python then, find out the average amount per month and then over a six month period.

    Totally lost on this one!

    I can get the file opened and I can get it to print out the column that I need to do the maths on.

    But I cannot get it too just print out say one month or the six month period, then I am totally lost on trying to get it to do the maths part of the problem. As in anything basic, like adding the values together, I have no idea.

    Going back over the power-point slides, I can't find out any information.

    Anyone able to help me out? Any links to something so I can find out more info.

    Cheers


Comments

  • Registered Users Posts: 6,022 ✭✭✭Talisman


    The standard library gives you a csv module. You want to read the columns into a list.
    import csv
    

    Python 3.4 added a statistics module to the standard library, so if you can read your csv file into a list you should be able to calculate the mean values.
    from statistics import mean
    L = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    result = mean(L)
    

    If you're not allowed to use the statistics module then you can use:
    result = sum(L) / len(L)
    

    If you're using Python 2.x then you won't have the statistcs module and you'll also have to tweak the calculation to get a floating point result:
    result = sum(L, 0.0) / len(L)
    

    In Python 2, print is a statement, in Python3 it's a function so the code is slightly different.

    Python 2:
    print result
    
    Python 3:
    print(result)
    

    If you need a resource to help you learn the language then check out Learn Python the Hard Way.


  • Registered Users Posts: 339 ✭✭duffman85


    The basic mathematical operations are described here:

    https://docs.python.org/2/tutorial/introduction.html#numbers


    There are other maths modules like https://docs.python.org/2/library/math.html#module-math and the ones Talisman mentioned


  • Registered Users Posts: 23,212 ✭✭✭✭Tom Dunne


    I think that it is vital before you go coding that you understand exactly the steps you want to take.

    So write out in pseudocode what you are going to do, from that you can then convert it to proper code.

    Essentially forget about Python until you have the pseudocode complete, don't let that distract you.


  • Closed Accounts Posts: 4,935 ✭✭✭TallGlass


    Talisman wrote: »
    The standard library gives you a csv module. You want to read the columns into a list.
    import csv
    

    Python 3.4 added a statistics module to the standard library, so if you can read your csv file into a list you should be able to calculate the mean values.
    from statistics import mean
    L = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    result = mean(L)
    

    If you're not allowed to use the statistics module then you can use:
    result = sum(L) / len(L)
    

    If you're using Python 2.x then you won't have the statistcs module and you'll also have to tweak the calculation to get a floating point result:
    result = sum(L, 0.0) / len(L)
    

    In Python 2, print is a statement, in Python3 it's a function so the code is slightly different.

    Python 2:
    print result
    
    Python 3:
    print(result)
    

    If you need a resource to help you learn the language then check out Learn Python the Hard Way.

    Sounds good, I will try get my output from the CSV file into a list and then do the calculations from there.

    My real problem, is just trying to visualize this in some way that it makes sense.

    I would rather do alot more research into Python, its seems interesting, but I am doing the course part time and there's not much time for me to play around with it. This project, has taking the wind out of my sails as I now have to figure out and do a crash course so I pass the module. Its not my typical style of learning, usually I like to get into the nuts and bolts.

    I feel like I have just learned to mix cement and now I am been asked to build a house ! Is this normal?


  • Closed Accounts Posts: 4,935 ✭✭✭TallGlass


    OSI wrote: »
    What was your previous assignment?

    This isn't a hard problem to solve and is the sort of thing I'd expect to see a student to be able to do within their first year of a course.

    This is my first one, I get that it should be easy ! I started just on the 8 of September with no experience. I'm a slow learner as I in would like to know fundamentals clearly first before moving to big ideas.

    Maybe I am simplifying it too much. The file for the CSV, is a stock file for Google from 2008-2012, with various fields.

    I have to get Python to tell me, first the average value for closing per month for every month. Then I have to get the average for six months and then get Python to tell the user the best six month period and worst six month period.

    1. I need to open the file (this I can do)
    2. I need to sort the dates
    3. I need to extract the values for closing on that day
    4. I need to calculate the average for that month
    5. I need to repeat this each month
    6. Print the output of my findings
    7. I need to sort the dates
    8. I need to extract the values
    9. I need to calculate the average for that six month period
    10. I need to compare the difference and decide what is the best and worst.
    11. Print the out of my findings

    From steps 2 to 11 (excluding the print I should be fine there) I am lost, I am going over the Powerpoints, but its not clearly defined for me, as in this is how you search the file by date etc... Its all just a little messy and has me lost.


  • Advertisement
  • Closed Accounts Posts: 4,935 ✭✭✭TallGlass


    OSI wrote: »
    What course is this?

    If this is your first assignment, it reminds me of the type that they used to give 3 months, and go over the necessary material through lectures over that period. So you did the different parts of it as you learned it. How long do you have?

    First year of a computer science style course in DIT. I have three weeks ! Well maybe two from now. Not alot at all. Really stuck, I need to figure things out quick or I lose out on 25% of marks available. I knew part time be hard, just not this hard.


  • Registered Users Posts: 23,212 ✭✭✭✭Tom Dunne


    TallGlass wrote: »
    This is my first one, I get that it should be easy ! I started just on the 8 of September with no experience. I'm a slow learner as I in would like to know fundamentals clearly first before moving to big ideas.

    You are not a slow learner, you are just a learner. Like all of us, so don't sweat it at this very early stage in your studies.
    TallGlass wrote: »
    1. I need to open the file (this I can do)
    2. I need to sort the dates
    3. I need to extract the values for closing on that day
    4. I need to calculate the average for that month
    5. I need to repeat this each month
    6. Print the output of my findings
    7. I need to sort the dates
    8. I need to extract the values
    9. I need to calculate the average for that six month period
    10. I need to compare the difference and decide what is the best and worst.
    11. Print the out of my findings

    Ok, that's a good start. However, it's a list of steps, with some order (not enough). You need to refine it to something like this:
    Open File
    Begin Loop
    Read in list of values
    Put them into a suitable construct that will allow sorting
    End Loop
    Begin Sort Loop
    Sort array
    ... and so on.
    TallGlass wrote: »
    From steps 2 to 11 (excluding the print I should be fine there) I am lost, I am going over the Powerpoints, but its not clearly defined for me, as in this is how you search the file by date etc... Its all just a little messy and has me lost.

    Seriously, forget about Powerpoints, forget about Python, focus on the job you have to do.

    Welcome to life. Nothing is clearly defined. :D

    (I realise I am being a bit of a dick vague here. But if I, or anybody else tells you the answer straight out, you will have learned nothing. And besides, I enjoy torturing people)


  • Closed Accounts Posts: 4,935 ✭✭✭TallGlass


    Tom Dunne wrote: »
    You are not a slow learner, you are just a learner. Like all of us, so don't sweat it at this very early stage in your studies.



    Ok, that's a good start. However, it's a list of steps, with some order (not enough). You need to refine it to something like this:





    Seriously, forget about Powerpoints, forget about Python, focus on the job you have to do.

    Welcome to life. Nothing is clearly defined. :D

    (I realise I am being a bit of a dick vague here. But if I, or anybody else tells you the answer straight out, you will have learned nothing. And besides, I enjoy torturing people)

    Ok thanks for the reply, mind you I am not looking for the answer! More so looking for the right direction to go so I don't fail it completely.

    I might aswell forget about Powerpoint, the slides are useless to me! The book I got has the same style question, but it gives no solution.

    I'll just have to figure it out, he mention that it should be easy and its stuff we covered.

    I don't want to become one of these people, who says coding is hard and never really gave it a go. I don't think its hard at the moment, I am just confused and its burly.


  • Closed Accounts Posts: 758 ✭✭✭Rakish Paddy


    TallGlass wrote: »
    First year of a computer science style course in DIT.

    Are you doing DT249? When I started it, we were taught programming & algorithms using C in first year so unfortunately I can't be of much help with Python (and others seem to be giving you good advice anyway).

    I just wanted to give you some encouragement - I'm now working on my final project with all other modules completed, and I went into it with zero programming knowledge. It has been challenging at times, but it's all very doable and those semesters go by quicker than you might think.

    If you're doing the course that I think you are, you have a few options if you're really struggling with a particular programming assignment:

    1. Go along on the lab night (they used to be Monday nights for us - I assume they still happen) and ask the lab tutor if he/she can point you in the right direction with the assignment.

    2. If you're still really stuck and having a hard time, talk to the lecturer. Either after class, during the break, or by email. Any time I needed to communicate directly with a lecturer, I found them to generally be very approachable and helpful. You're better off doing this early than leaving it to the day it is due.


  • Closed Accounts Posts: 4,935 ✭✭✭TallGlass


    So one month on, and I have been trying with this and just getting it wrong. Have asked for help on it but it just aint going to happen, I got this much basically started it again, but when I try to test it just prints a blank, as in nothing, no error or anything! I don't want to move onto anything else, and someone mentioned writing pseudocode but I don't understand that. Also, I knows a month on but I have been busy with work and other things and then heading into college part time. Anyone got any other advise?

    __author__ = 'x'

    import csv

    googleprices = open('/home/x/Downloads/googlePrices.csv','r')
    file_reader = csv.reader(googleprices)

    def get_data_list(file_reader):
    data_file = open(file_reader, "r")
    data_list = []
    for line_str in data_file:
    data_list.append(line_str.strip().split(','))
    return data_list

    print(data_file)


  • Advertisement
  • Registered Users Posts: 10,489 ✭✭✭✭28064212


    Indentation is important in python. Use [noparse]
    
    [/noparse] tags. I assume it was supposed to look like this?
    
    __author__ = 'x'
    
    import csv
    
    googleprices = open('/home/x/Downloads/googlePrices.csv','r')
    file_reader = csv.reader(googleprices)
    
    def get_data_list(file_reader):
    	data_file = open(file_reader, "r")
    	data_list = []
    	for line_str in data_file:
    		data_list.append(line_str.strip().split(','))
    	return data_list
    
    print(data_file)
    

    You've defined a function called get_data_list, but you haven't called it. The code inside the function doesn't get executed unless it's called.

    You're trying to use the csv module in the first few lines, but your function get_data_list doesn't use it at all, and attempts to read the file and do the parsing of the csv by itself. If you want to use the csv module, read the documentation for it, specifically the reader function, and the Examples

    Did you read http://learnpythonthehardway.org/book/ suggested to you above? It takes you through python from first principles, and fills it the gaps you're missing (like what a function is)

    Boardsie Enhancement Suite - a browser extension to make using Boards on desktop a better experience (includes full-width display, keyboard shortcuts, dark mode, and more). Now available through your browser's extension store.

    Firefox: https://addons.mozilla.org/addon/boardsie-enhancement-suite/

    Chrome/Edge/Opera: https://chromewebstore.google.com/detail/boardsie-enhancement-suit/bbgnmnfagihoohjkofdnofcfmkpdmmce



  • Registered Users Posts: 6,022 ✭✭✭Talisman


    TallGlass wrote: »
    So one month on, and I have been trying with this and just getting it wrong. Have asked for help on it but it just aint going to happen, I got this much basically started it again, but when I try to test it just prints a blank, as in nothing, no error or anything!
    There should be an error because if the code is correctly formatted as you are trying to print an undefined variable data_file. It's only defined within the get_data_list function.
    I don't want to move onto anything else, and someone mentioned writing pseudocode but I don't understand that.
    Pseudo code means writing out the logic of the program so you get an idea of what you are trying to do. The actual code should then follow this structure.
    Also, I knows a month on but I have been busy with work and other things and then heading into college part time. Anyone got any other advise?
    Baby steps ...

    Your data is in a csv file.
    Does it contain a header row?
    Is the data already sorted by date?

    The answers to these questions can simplify some of the logic for the program.

    If the data is already sorted then too can perform your calculations as you go.

    Algorithm / Pseudo code:
    Open the file
    Read the content line by line
    For each line:
      Determine the date
      Get the data value for the total calculation
      If the month hasn't changed:
        Add the value to the total for this month
        Increment the counter for the number of data items for this month
      Otherwise:
        Set the total for this new month to the value
        Initialise the counter for the number of data items for this month to 1
    Close the file
    
    For each month:
      Perform the average calculation (total for month / number of data items for month)
    
    Print the results
    

    The above gives you the outline of the program, if you can turn it into Python code you can solve the problem.

    If your csv file contains a header row you can use it as a key for a dictionary. See Python dictionary data type.

    A simple file: dummy.csv
    id,item,value,date
    1,apple,2.00,2015-09-10
    2,ball,5.00,2015-09-11
    3,carrot,1.00,2015-10-10
    4,donut,4.00,2015-10-11
    5,elephant,8.00,2015-11-12
    6,fish,12.00,2015-11-13
    

    Python code:
    import csv
    with open("dummy.csv", "rb") as csvFile:
    	csvContent = csv.DictReader( csvFile )
    	for line in csvContent:
    		print line['date'], ":", line['value']
    

    Output:
    2015-09-10 : 2.00
    2015-09-11 : 5.00
    2015-10-10 : 1.00
    2015-10-11 : 4.00
    2015-11-12 : 8.00
    2015-11-13 : 12.00
    

    You can use the datetime.strptime method from the datetime module to convert the string to a proper datetime object.

    e.g. For the example above:
    from datetime import datetime
    thisMonth = datetime.strptime(line['date'], "%Y-%m-%d").month
    

    You'll be surprised how few lines of code are required to get a working solution.


Advertisement