Loading data from .csv file into new variables | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Loading data from .csv file into new variables

Hello, I have an exercise to load data from .csv file and count NPV for these values. I've done it, but I have a problem with loading seperate values into my NPV. Here's my code: __________________________________________________ from pandas import read_csv from numpy import npv def maxnpv(): if (npvA > npvB) and (npvA > npvC): print("Most profitable is A.") elif (npvB > npvA) and (npvB > npvC): print("Most profitable is B.") else: print("Most profitable is C.") fund = read_csv("inwestycje.csv", delimiter=',') funds = [tuple(x) for x in fund.values] project1 = tuple(funds[0]) project2 = tuple(funds[1]) project3 = tuple(funds[2]) npvA = npv(project1[1], [project1[2], project1[3], project1[4], project1[5], project1[6], project1[7], project1[8]]) npvB = npv(project2[1], [project2[2], project2[3], project2[4], project2[5], project2[6], project2[7], project2[8]]) npvC = npv(project3[1], [project3[2], project3[3], project3[4], project3[5], project3[6], project3[7], project3[8]]) print("NPV for project 'A': PLN {:,}".format(round(npvA, 2))) print("NPV for project 'B': PLN {:,}".format(round(npvB, 2))) print("NPV for project'C': PLN {:,}".format(round(npvC, 2))) maxnpv() __________________________________________________ And this is how the .csv file looks like: https://i.imgur.com/B3uyLKz.jpg __________________________________________________ As you can see, I loaded the values manually into "npv", but I want to make it automatic, so when the file doesn't consist 5 values, it should count it anyway with given amount. How can I do it? Any ideas? Thanks!

13th Dec 2018, 2:34 PM
Konik
Konik - avatar
2 Answers
+ 2
You can shorten it this way: npvA = npv(project1[1], project1[2:]) npvB = npv(project2[1], project2[2:]) npvC = npv(project3[1], project3[2:]) Here is version for read n projects, except maxnpv() (executable online) https://onlinegdb.com/Sk6Lf6GxN or on sololearn storage https://code.sololearn.com/cyjIS6nRGtve/#py
15th Dec 2018, 6:09 PM
zemiak
+ 1
Use python's for-loop shorthand: https://code.sololearn.com/cpwVcF0Mr078/#py Hope this helps.
15th Dec 2018, 7:58 AM
Hanz
Hanz - avatar