Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7181409
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T17:33:04+00:00 2026-05-28T17:33:04+00:00

I apologize if this has been asked here – I’ve hunted around here and

  • 0

I apologize if this has been asked here – I’ve hunted around here and in the Tentative NumPy Tutorial for an answer.

I have 2 numpy arrays. The first array is similar to:

1 0 0 0 0

2 0 0 0 0

3 0 0 0 0

4 0 0 0 0

5 0 0 0 0

6 0 0 0 0

7 0 0 0 0

8 0 0 0 0

(etc… It’s ~700×10 in actuality)

I then have a 2nd array similar to

3 1

4 18

5 2

(again, longer – maybe 400 or so rows)

The first column of the 2nd array is always completely contained within the first column of the first array

What I’d like to do is to insert the 2nd column of the 2nd array into that first array as part of an existing column, i.e:

array a:

1 0 0 0 0

2 0 0 0 0

3 1 0 0 0

4 18 0 0 0

5 2 0 0 0

6 0 0 0 0

7 0 0 0 0

8 0 0 0 0

(I’d be filling in each of those columns in turn, but each covers a different range within the original)

My first try was along the lines of a[b[:,0],1]=b[:,1] which puts them into the indices of b, not the values (ie, in my example above instead of filling in rows 3, 4, and 5, I filled in 2, 3, and 4). I should have realized that!

Since then, I’ve tried to make it work pretty inelegantly with where(), and I think I could make it work by finding the difference in the starting values of the first columns.

I’m new to python, so perhaps I’m overly optimistic – but it seems like there should be a more elegant way and I’m just missing it.

Thanks for any insights!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T17:33:06+00:00Added an answer on May 28, 2026 at 5:33 pm

    If the numbers in the first column of a are in sorted order, then you could use

    a[a[:,0].searchsorted(b[:,0]),1] = b[:,1]
    

    For example:

    import numpy as np
    
    a = np.array([(1,0,0,0,0),
                  (2,0,0,0,0),
                  (3,0,0,0,0),
                  (4,0,0,0,0),
                  (5,0,0,0,0),
                  (6,0,0,0,0),
                  (7,0,0,0,0),
                  (8,0,0,0,0),
                  ])
    
    b = np.array([(3, 1),
                  (5, 18),
                  (7, 2)])
    
    a[a[:,0].searchsorted(b[:,0]),1] = b[:,1]
    print(a)
    

    yields

    [[ 1  0  0  0  0]
     [ 2  0  0  0  0]
     [ 3  1  0  0  0]
     [ 4  0  0  0  0]
     [ 5 18  0  0  0]
     [ 6  0  0  0  0]
     [ 7  2  0  0  0]
     [ 8  0  0  0  0]]
    

    (I changed your example a bit to show that the values in b‘s first column do not have to be contiguous.)


    If a[:,0] is not in sorted order, then you could use np.argsort to workaround this:

    a = np.array( [(1,0,0,0,0),
                   (2,0,0,0,0),
                   (5,0,0,0,0),
                   (3,0,0,0,0),
                   (4,0,0,0,0),
                   (6,0,0,0,0),
                   (7,0,0,0,0),
                   (8,0,0,0,0),
                   ])
    
    b = np.array([(3, 1),
                  (5, 18),
                  (7, 2)])
    
    perm = np.argsort(a[:,0])
    a[:,1][perm[a[:,0][perm].searchsorted(b[:,0])]] = b[:,1]
    print(a)
    

    yields

    [[ 1  0  0  0  0]
     [ 2  0  0  0  0]
     [ 5 18  0  0  0]
     [ 3  1  0  0  0]
     [ 4  0  0  0  0]
     [ 6  0  0  0  0]
     [ 7  2  0  0  0]
     [ 8  0  0  0  0]]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If this has been asked before I sincerely apologize. After around half an hour
I'm sure this has already been asked and answered so I apologize in advance
First off, let me apologize if this has been asked already, but I can’t
Apologies if this has been asked before (I couldn't find the answer anywhere), but
I know this question has been kind of asked and have looked into the
I apologize if this has been asked before, but I haven't quite found the
First, I apologize if this has been asked before - indeed I'm sure it
I apologize if this has already been asked a different way but I couldn't
I know that this question has been asked loads of times, but I have
This has almost certainly been asked here before, so apologies if it's a duplicate.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.