jeudi 27 octobre 2016

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.

Aucun commentaire:

Enregistrer un commentaire