Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
numpy adding columns
#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


Messages In This Thread
numpy adding columns - by rwahdan - Sep-21-2021, 03:33 PM
RE: numpy adding columns - by jefsummers - Sep-21-2021, 03:56 PM
RE: numpy adding columns - by rwahdan - Sep-21-2021, 04:04 PM
RE: numpy adding columns - by rwahdan - Sep-21-2021, 06:26 PM
RE: numpy adding columns - by deanhystad - Sep-21-2021, 08:25 PM

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