3

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.

1
  • 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,15 Commented May 13, 2015 at 19:27

1 Answer 1

3

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]

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.