Louise Stevens
Louise Stevens

Reputation: 85

Conditional statements in a loop

I have a csv file with 5 columns and 13 rows that looks like this:

site experiment length width height

1   1   2.2 1.3 9.6
1   2   2.1 2.2 7.6
1   3   2.7 1.5 2.2
2   1   3   4.5 1.5
2   2   3.1 3.1 4
2   3   2.5 2.8 3
3   1   1.9 1.8 4.5
3   2   1.1 0.5 2.3
3   3   3.5 2   7.5
4   1   2.9 2.7 3.2
4   2   4.5 4.8 6.5
4   3   1.2 1.8 2.7

Length/width/height are that of plants.

For each row in the dataset I want to create a conditional code to see if the plant is tall (height > 5), medium (2 <= height < 5), or short (height < 2), and then determine the total amount of carbon each plant.

Total carbon in plant = 1.8 + 2 * log(volume) where volume=length x width x height.

I then want to store this information as table in a nested list where the first column has the experiment number, the second column contains the string 'tall', 'medium' or 'short' depending on the height of the plant, and the third column contains the carbon content of the plant.

This is my code so far:

from __future__ import division
import math
import numpy
shrub_exp=numpy.loadtxt("/Users/louisestevens/Downloads/shrub_volume_experiment.csv",dtype=float,delimiter=',',skiprows=1,usecols=(2,3,4))
for rows in shrub_exp:
    print(rows)

height=(shrub_exp,4)
def height_test(height):
    if height > 5:
        return 'Tall'
    elif 2 <= height < 5:
        return 'Medium'
    else:
        return 'Short'
for x in height:
    print(height_test(x))

for x,y,z in shrub_exp:
    volume=(x*y*z)
    total_carbon=1.8 + 2 * math.log(volume)
    print(total_carbon)

I am unsure if I have selected the height column - which is the final column - correctly and how to store this information in a nested list.

Please may I have some pointers as to how to write this script concisely and effectively.

Upvotes: 0

Views: 201

Answers (2)

Rolf of Saxony
Rolf of Saxony

Reputation: 22443

Without resorting to numpy, the following code is one way of getting your result. Assuming csv file called shrub.csv in the local directory, as follows:

1,1,2.2,1.3,9.6
1,2,2.1,2.2,7.6
1,3,2.7,1.5,2.2
2,1,3,4.5,1.5
2,2,3.1,3.1,4
2,3,2.5,2.8,3
3,1,1.9,1.8,4.5
3,2,1.1,0.5,2.3
3,3,3.5,2,7.5
4,1,2.9,2.7,3.2
4,2,4.5,4.8,6.5
4,3,1.2,1.8,2.7

import math
f=open('shrub.csv')
shrub_exp=f.readlines()
f.close()

def height_test(height):
    if height > 5:
        return 'Tall'
    elif height >= 2:
        return 'Medium'
    else:
        return 'Short'
res=[]
for row in shrub_exp:
    site,exp,leng,wid,hgt = row.split(',')
    volume=(float(leng)*float(wid)*float(hgt))
    total_carbon=1.8 + 2 * math.log(volume)
    res.append([exp, height_test(float(hgt)), total_carbon])
for r in res:
    print r

Note there is no error checking for the data.

['1', 'Tall', 8.425169446611104]
['2', 'Tall', 8.917085904771866]
['3', 'Medium', 6.174348482965436]
['1', 'Short', 7.8163095871050965]
['2', 'Medium', 9.098197168204184]
['3', 'Medium', 7.889044875446846]
['1', 'Medium', 7.267435895701576]
['2', 'Medium', 2.270144244358967]
['3', 'Tall', 9.721626339195156]
['1', 'Medium', 8.242226639616785]
['2', 'Tall', 11.688990983183421]
['3', 'Medium', 5.326719989412714]

Upvotes: 0

CAB
CAB

Reputation: 1147

shhrub_exp is a list of lists, and each of those lists is a row from the CSV. The line

height=(shrub_exp,4)

creates a new tuple with two elements, the first is shrub_exp and the second is the number 4. That does nothing for you.

If you want to process height from each row;

for row in shrub_exp:
    print( height_test(row[2]) )

Why 2? Because you skipped columns 0 and 1 when you loaded the file. So, column 4 in the file is now column 2 in the row data list.

Your final for loop unpacks each row into x,y,z. z then is height. To capture the output in a similar lists of lists, you could do this;

results = [] # start with empty list
for length,width,height in shrub_exp:
    volume=(length*width*height)
    total_carbon=1.8 + 2 * math.log(volume)
    results.append( [height_test(height) , volume, total_carbon] )  # add new row to the result

Upvotes: 1

Related Questions