Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
numpy adding columns
#1
Hi,

I am trying to add columns to a numpy array but i am getting an error

import numpy as np
numbers = np.array([
    [2,4],
    [8,10],
    [50,100]
    ])

add = numbers[:,0] + numbers[:,1]
mul = numbers[:,0] * numbers[:,1]

numbers = np.hstack((numbers,add))
numbers = np.hstack((numbers,mul))
i am getting this error:
Error:
Traceback (most recent call last): File "main.py", line 11, in <module> numbers = np.hstack((numbers,add)) File "<__array_function__ internals>", line 5, in hstack File "/usr/lib/python3.8/site-packages/numpy/core/shape_base.py", line 346, in hstack return _nx.concatenate(arrs, 1) File "<__array_function__ internals>", line 5, in concatenate ValueError: all the input arrays must have same number of dimensions, but the array at index 0 has 2 dimension(s) and the array at index 1 has 1 dimension(s) ** Process exited - Return Code: 1 ** Press Enter to exit terminal
Reply
#2
Not sure if this is what you are looking for, but here goes

import numpy as np
numbers = np.array([
    [2,4],
    [8,10],
    [50,100]
    ])
 
add = numbers[:,0] + numbers[:,1]
mul = numbers[:,0] * numbers[:,1]
print(numbers)
print(add.transpose()) 
numbers = np.append(numbers,add.transpose())
numbers = np.append(numbers,mul.transpose())
print(numbers)
Output:
[[ 2 4] [ 8 10] [ 50 100]] [ 6 18 150] [ 2 4 8 10 50 100 6 18 150 8 80 5000]
Reply
#3
(Sep-21-2021, 03:56 PM)jefsummers Wrote: Not sure if this is what you are looking for, but here goes

import numpy as np
numbers = np.array([
    [2,4],
    [8,10],
    [50,100]
    ])
 
add = numbers[:,0] + numbers[:,1]
mul = numbers[:,0] * numbers[:,1]
print(numbers)
print(add.transpose()) 
numbers = np.append(numbers,add.transpose())
numbers = np.append(numbers,mul.transpose())
print(numbers)
Output:
[[ 2 4] [ 8 10] [ 50 100]] [ 6 18 150] [ 2 4 8 10 50 100 6 18 150 8 80 5000]

Thanks for the reply. What I am looking for is to add the 2 new arrays (add, mul) as columns so the answer should be (columns):

Output:
X Y SUM Mul 2 4 6 8 8 10 18 80 50 100 150 5000
Reply
#4
I just a solution but if anyone can think of a better way please share...

import numpy as np
numbers = np.array([
    [2,4],
    [8,10],
    [50,100]
    ])
  
add = numbers[:,0] + numbers[:,1]
add = add.reshape(3,1)
mul = numbers[:,0] * numbers[:,1]
mul = mul.reshape(3,1)
numbers_add = np.hstack((numbers, add))
numbers_mul = np.hstack((numbers_add, mul))
print(numbers_mul)
the results:
Output:
[[ 2 4 6 8] [ 8 10 18 80] [ 50 100 150 5000]] ** Process exited - Return Code: 0 ** Press Enter to exit terminal
Reply
#5
import numpy as np

numbers = np.array([[2,4], [8,10], [50,100]])
table = np.c_[numbers, np.sum(numbers, axis=1), np.prod(numbers, axis=1)]
print('     A    B    +    *', table, sep='\n')
Output:
A B + * [[ 2 4 6 8] [ 8 10 18 80] [ 50 100 150 5000]]
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Numpy] How to store different data type in one numpy array? water 7 545 Mar-26-2024, 02:18 PM
Last Post: snippsat
  Merging rows and adding columns based on matching index pythonnewbie78 3 812 Dec-24-2023, 11:51 AM
Last Post: Pedroski55
  Numpy returns "TypeError: unsupported operand type(s) for *: 'numpy.ufunc' and 'int'" kalle 2 2,604 Jul-19-2022, 06:31 AM
Last Post: paul18fr
  Adding data in 3D array from 2D numpy array asmasattar 0 2,218 Jul-23-2020, 10:55 AM
Last Post: asmasattar
  "erlarge" a numpy-matrix to numpy-array PhysChem 2 2,976 Apr-09-2019, 04:54 PM
Last Post: PhysChem
  Adding Columns to CSV using iterator pstarrett 10 27,526 Jan-22-2018, 02:37 AM
Last Post: pstarrett

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020