Calculation Between Two Columns In Python?
When I tried to do some calculation between two columns (like division), I get an error: column_ratio[x]=(float(column1[y]))/(float(column2[z])) TypeError: tuple indices must be
Solution 1:
When you parse with the CSV reader, it has no way of knowing that the data should be an integer, a float, a string, etc. All comma-delimited data is parsed as a string, and it's up to you to convert it to the right type. You'll need to coerce the string to an integer to use it as an index.
Take the following CSV:
Index One, Index Two, Index Three
0,1,2
When parsing it:
x, y, z = reader.next() # ['0', '1', '2']print my_tuple[x] # TypeError: tuple indices must be integers, not strprint my_tuple[int(x)] # Expected result
Post a Comment for "Calculation Between Two Columns In Python?"