I have 2 csv files with 16 columns and 146 rows. I am trying to do the following (This is not actual data):
a = [1, 2, 3, 4, 5, 6]
b = [10, 20, 30, 40, 50, 60]
final output of the script intended:
x = [ 11, 22, 33, 22, 22.5, 33] # basically the last half of the array needs to be divided by 2
I have tried out the follwoing code:
import csv
import numpy as np
import sys
data = np.genfromtxt('./test1.csv', dtype=float, delimiter=',')
data_sys = np.genfromtxt('.test2.csv', dtype=float, delimiter=',')
z = np.add (data, data_sys)
np.savetxt("new_before_avg.csv", z, delimiter= ',')
z[:,8:15] = z[:,8:15]/2
np.savetxt("new_after_avg.csv"], z, delimiter= ",")
Problem is, I am seeing the final output as expected except for the last column (column 15). It is just added up, did not divide by 2.
I thought my indexing was right. Please help.
asked May 13, 2015 at 19:20
user3285014
3191 gold badge3 silver badges12 bronze badges
-
if there are 16 columns why do you slice 8:15? Remember python slices go up the index before the end! i.e 8:16 -> 8,9,10,11,12,13,14,15paddyg– paddyg2015年05月13日 19:27:15 +00:00Commented May 13, 2015 at 19:27
1 Answer 1
z[:,8:15] indexes up to but not including the last column (the 16th, indexed at z[:,15]).
Use z[:, 8:] or z[:,8:16]
answered May 13, 2015 at 19:26
xnx
25.7k11 gold badges75 silver badges118 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
lang-py