Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to replace only 1d values in 2d array after filter using numpy in python

How to replace only 1d values in 2d array after filter using numpy in python

Scheduled Pinned Locked Moved General and Desktop
2 Posts 2 Posters 821 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Ravi_exact
    wrote on 29 Oct 2014, 09:43 last edited by
    #1

    Hi,
    How to replace only 1d values in 2d array after filter using numpy in python without loop i.e in pythonic way.

    I want to filter only t2 rows and replace values in second column ( middle column ).

    example:

    x = np.array([['t1',10,20],['t2',11,22],['t2',12,23], ['t3',21,32]])

    x
    array([['t1', '10', '20'],
    ['t2', '11', '22'],
    ['t2', '12', '23'],
    ['t3', '21', '32']],
    dtype='|S2')

    replace_value_for_t2_col1 = [100,101]

    so expected output is

    array([['t1', '10', '20'],
    ['t2', '100', '22'],
    ['t2', '101', '23'],
    ['t3', '21', '32']],
    dtype='|S2')

    What i tried was,

    Filter rows for t2

    x[x[:,0]=='t2']

    array([['t2', '11', '22'],
    ['t2', '12', '23']],
    dtype='|S2')

    x[x[:,0]=='t2'][:,1] = np.array([101,102])

    Seems right, but not replacing. i guess it make a copy when we slice using [:,1], so its not changing in same array.

    Please suggest me any brilliant ,simple and pythonic ideas, techniques.

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 29 Oct 2014, 23:54 last edited by
      #2

      Hi,

      You're currently out of the forum scope. You should rather go to a python forum for that question.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0

      1/2

      29 Oct 2014, 09:43

      • Login

      • Login or register to search.
      1 out of 2
      • First post
        1/2
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved