Skip to content Skip to sidebar Skip to footer

Valueerror: Invalid Literal For Int() With Base 10: 'height (mm)'

import csv from decimal import * def mean(data_set): return Decimal(sum(data_set)) / len(data_set) def variance(data_set): mean_res = mean(data_set) differences = []

Solution 1:

I think your problem is that you read the header (first line) which contain words, and you try to parse it as int.

Try to add

next(csv_reader, None)

Before your loop to skip first row.

As to "How do I tackle that error", for next time simply use print:

print row[1]

Right before the line that produced the error. See what you actually try to convert to int..

Solution 2:

use height_data[:-11] or height_data[:-12] (if you have a space between you number and Height (mm)

if __name__ == "__main__":
    withopen("dog_data.csv", "r") as csv_file:
        csv_reader = csv.reader(csv_file)
        height_data = []
        for row in csv_reader:
            height_data.append(int(row[1]))[:-12] # depends on you want to through away how many characters print"Mean: {}".format(mean(height_data))
        print"Variance: {}".format(variance(height_data))
        print"Standard Deviation:{}".format(standard_deviation(height_data))

Updates:

Assume this is what in you csv data

Dog Height(mm)
Sammi 600 
Doyle 470
Margo 170
Cookie 430
Dandriff 300

Save both of us some trouble and life is too short, use pandas:

import pandas as pd
df = pd.read_clipboard()

df

Out[49]: 
        Dog  Height(mm)
0     Sammi         600
1     Doyle         470
2     Margo         170
3    Cookie         430
4  Dandriff         300

df['Height(mm)'].mean()
Out[50]: 394.0

df['Height(mm)'].std()
Out[51]: 164.7118696390761

Post a Comment for "Valueerror: Invalid Literal For Int() With Base 10: 'height (mm)'"