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

  • Home
  • SEARCH
  • 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 8448987
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T10:34:34+00:00 2026-06-10T10:34:34+00:00

This question is about filtering a NumPy ndarray according to some column values. I

  • 0

This question is about filtering a NumPy ndarray according to some column values.

I have a fairly large NumPy ndarray (300000, 50) and I am filtering it according to values in some specific columns. I have ndtypes so I can access each column by name.

The first column is named category_code and I need to filter the matrix to return only rows where category_code is in ("A", "B", "C").

The result would need to be another NumPy ndarray whose columns are still accessible by the dtype names.

Here is what I do now:

index = numpy.asarray([row['category_code'] in ('A', 'B', 'C') for row in data])
filtered_data = data[index]

List comprehension like:

list = [row for row in data if row['category_code'] in ('A', 'B', 'C')]
filtered_data = numpy.asarray(list)

wouldn’t work because the dtypes I originally had are no longer accessible.

Are there any better / more Pythonic way of achieving the same result?

Something that could look like:

filtered_data = data.where({'category_code': ('A', 'B','C'})

Thanks!

  • 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-06-10T10:34:36+00:00Added an answer on June 10, 2026 at 10:34 am

    You can use the NumPy-based library, Pandas, which has a more generally useful implementation of ndarrays:

    >>> # import the library
    >>> import pandas as PD
    

    Create some sample data as python dictionary, whose keys are the column names and whose values are the column values as a python list; one key/value pair per column

    >>> data = {'category_code': ['D', 'A', 'B', 'C', 'D', 'A', 'C', 'A'], 
                'value':[4, 2, 6, 3, 8, 4, 3, 9]}
    
    >>> # convert to a Pandas 'DataFrame'
    >>> D = PD.DataFrame(data)
    

    To return just the rows in which the category_code is either B or C, two steps conceptually, but can easily be done in a single line:

    >>> # step 1: create the index 
    >>> idx = (D.category_code== 'B') | (D.category_code == 'C')
    
    >>> # then filter the data against that index:
    >>> D.ix[idx]
    
            category_code  value
       2             B      6
       3             C      3
       6             C      3
    

    Note the difference between indexing in Pandas versus NumPy, the library upon which Pandas is built. In NumPy, you would just place the index inside the brackets, indicating which dimension you are indexing with a “,”, and using “:” to indicate that you want all of the values (columns) in the other dimension:

    >>>  D[idx,:]
    

    In Pandas, you call the the data frame’s ix method, and place only the index inside the brackets:

    >>> D.loc[idx]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Earlier this week I ask a question about filtering out duplicate values in sequence
I have a question about filtering in my dataset. My dataset look like this:
This question about Timers for windows services got me thinking: Say I have (and
I've this question about pass some instances by ref or not: here is my
I have seen this question about deploying to WebSphere using the WAS ant tasks.
I have a simple question about filtering attributes in WEKA. Let's say I have
I have a question about how to do data filtering with RequestFactory in GWT.
I have a question about nHibernate and filtering. I am using nHibernate 2.2 but
I have a question about making a xpath expression for filtering resources by a
Followed this question about delayed_job and monit Its working on my development machine. But

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.