简体   繁体   中英

python numpy array data manipulation

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.

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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM