python - csv - take away data of one column from another -
i want take value of column 4 column 3 (their both integers), have.
with open('classes.csv', 'rt')as f: reader=csv.reader(f) people=[] column in reader: people.append(column[0:8]) difference = [x[3] x in people] - [x[4] x in people] print(difference)
i error typeerror: unsupported operand type(s) -: 'list' , 'list' when this. know why error can't think of way around it.
any great!
like @peter wood said in comment, need subtract inside of 1 list comprehension, not doing 2 list comprehension results in list - list
. need convert numbers.
difference = [int(x[3]) - int(x[4]) x in people]
if want stuff this, better off using numpy
or pandas
.
using numpy , it's genfromtxt
function:
import numpy np people = np.genfromtxt('classes.txt', delimiter=',', dtype=none) difference = people[:, 3] - people[:, 4]
here people
2 dimensional numpy array, first index going on rows, second on columns [:, 3]
takes every row of thirds column.
the dtype=none
option lets genfromtxt
automatically decide type each column, default float
.
Comments
Post a Comment