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 3485738
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T10:56:37+00:00 2026-05-18T10:56:37+00:00

Assuming I have a numpy array like: [1,2,3,4,5,6] and another array: [0,0,1,2,2,1] I want

  • 0

Assuming I have a numpy array like:
[1,2,3,4,5,6]
and another array:
[0,0,1,2,2,1]
I want to sum the items in the first array by group (the second array) and obtain n-groups results in group number order (in this case the result would be [3, 9, 9]). How do I do this in numpy?

  • 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-18T10:56:38+00:00Added an answer on May 18, 2026 at 10:56 am

    There’s more than one way to do this, but here’s one way:

    import numpy as np
    data = np.arange(1, 7)
    groups = np.array([0,0,1,2,2,1])
    
    unique_groups = np.unique(groups)
    sums = []
    for group in unique_groups:
        sums.append(data[groups == group].sum())
    

    You can vectorize things so that there’s no for loop at all, but I’d recommend against it. It becomes unreadable, and will require a couple of 2D temporary arrays, which could require large amounts of memory if you have a lot of data.

    Edit: Here’s one way you could entirely vectorize. Keep in mind that this may (and likely will) be slower than the version above. (And there may be a better way to vectorize this, but it’s late and I’m tired, so this is just the first thing to pop into my head…)

    However, keep in mind that this is a bad example… You’re really better off (both in terms of speed and readability) with the loop above…

    import numpy as np
    data = np.arange(1, 7)
    groups = np.array([0,0,1,2,2,1])
    
    unique_groups = np.unique(groups)
    
    # Forgive the bad naming here...
    # I can't think of more descriptive variable names at the moment...
    x, y = np.meshgrid(groups, unique_groups)
    data_stack = np.tile(data, (unique_groups.size, 1))
    
    data_in_group = np.zeros_like(data_stack)
    data_in_group[x==y] = data_stack[x==y]
    
    sums = data_in_group.sum(axis=1)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Assuming I have a tree structure UL --LI ---INPUT (checkbox) And I want to
Hej, assuming I have a code that looks like this: List<User> userList = GetUserByName
I have a Numpy array and a list of indices whose values I would
I have the following numpy array: # A B C Y my_arr = np.array([
Assuming I have a hash like this: $hash_ref = { 'hashes' => { 'h1'
Assuming I have a List<Stuff> listA that has some items in it. I create
Assuming I have a string $str = "abc*efg*hij*"; and an array $arr = array("123","456","789");
Assuming you have a project setup like this: -WebApp |_ requirements.txt |_ bootstrap.py (virtualenv
Assuming I have a bunch of timestamps like 11/05/2010 16:27:26.003, how do parse them
Assuming I have only the class name of a generic as a string in

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.