Reputation: 301
I have taken data from a csv file using numpy. numpy array has dimensions : 100*20. How do i take average of columns (say col 3,5,8) and replace them with a new column containing average of these 3 cols
If
col3 = 1,2,3,4
col5 = 2,3,4,8
col8 = 3,4,5,6
then I want to remove these 3 columns and insert a new column in which each entry contains an average of values in these 3 columns
I want to insert a new column: 2,3,4,6, delete previous 3 cols and dimension of final array to be 100*28
Is there any numpy function to do this ?
Upvotes: 5
Views: 20755
Reputation: 12590
a = np.arange(100*30).reshape(100,30) # change this to your own data
cols = [2, 4, 7] # columns to calculate averages, i.e. 3,5,8
b = a[:, cols] # data of the cols
c = b.mean(axis=1) # average
a_no_b = np.delete(a, cols, axis=1) # data without the cols
a_final = np.c_[a_no_b, c] # final result
Upvotes: 4
Reputation: 605
For columns axis = 0, so:
X = np.random.randint(0,101,shape=(100,50))
columns_average = X.mean(axis=0)
You get 1D array 'columns_average' with mean for every column, so 50 values.
Upvotes: 2