Series to Int with a csv

21 Views Asked by At

Completely new at working in python but giving it a shot.

I have a csv file that I'm reading in and one of the columns is called 'Left 5' and is datatype "object some examples of what data is contained in this column are the following “116-132” NaN NaN “116-121” “116-166” NaN “116-129” what I'm looking to do: Insert a column 'L5' with if the contents of 'Left 5' are NaN, 0, else the second number in 'Left 5' - the first number in 'Left 5' + 1. ie: 132-116+1, so 17 would be written in the cell

I've tried capturing the numbers as int L5A = int(batcsv['Left 5'].str[0:3]) L5B = int(batcsv['Left 5'].str[-3:])

TypeError: cannot convert the series to <class 'int'>

so then I tried capturing as a string then convert to an int

L5A = batcsv['Left 5'].str[0:3] L5B = batcsv['Left 5'].str[-3:]

then converting to int L5A = int(L5A) L5A = int(L5A)

it succeeds on capturing as a string but then it errors on the int() part with the same error TypeError: cannot convert the series to <class 'int'>

what am I missing to accompolish this?

0

There are 0 best solutions below