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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T12:28:43+00:00 2026-06-14T12:28:43+00:00

After seeing this question about replicating SQL select-statement-like behavior in Pandas, I added this

  • 0

After seeing this question about replicating SQL select-statement-like behavior in Pandas, I added this answer showing two ways that could shorten the verbose syntax given in the accepted answer to that question.

After playing around with them, my two shorter-syntax methods are significantly slower, and I am hoping someone can explain why

You can assume any functions used below are either from Pandas, IPython, or from the question and answers linked above.

import pandas
import numpy as np
N = 100000
df = pandas.DataFrame(np.round(np.random.rand(N,5)*10))

def pandas_select(dataframe, select_dict):
    inds = dataframe.apply(lambda x: reduce(lambda v1,v2: v1 and v2,
                           [elem[0](x[key], elem[1])
                           for key,elem in select_dict.iteritems()]), axis=1)
    return dataframe[inds]



%timeit _ = df[(df[1]==3) & (df[2]==2) & (df[4]==5)]
%timeit _ = df[df.apply(lambda x: (x[1]==3) & (x[2]==2) & (x[4]==5), axis=1)]

import operator
select_dict = {1:(operator.eq,3), 2:(operator.eq,2), 4:(operator.eq,5)}
%timeit _ = pandas_select(df, select_dict)

The output I get is:

In [6]: %timeit _ = df[(df[1]==3) & (df[2]==2) & (df[4]==5)]
100 loops, best of 3: 4.91 ms per loop

In [7]: %timeit _ = df[df.apply(lambda x: (x[1]==3) & (x[2]==2) & (x[4]==5), axis=1)]
1 loops, best of 3: 1.23 s per loop

In [10]: %timeit _ = pandas_select(df, select_dict)
1 loops, best of 3: 1.6 s per loop

I can buy that the user of reduce, operator functions, and just the function overhead from my pandas_select function could slow it down. But it seems excessive. Inside of my function, I’m using the same syntax, df[key] logical_op value, but it’s much slower.

I’m also puzzled why the apply version along axis=1 is so much slower. It should literally be just a shortening of the syntax, no?

  • 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-14T12:28:44+00:00Added an answer on June 14, 2026 at 12:28 pm

    When you write df[df.apply(lambda x: (x[1]==3) & (x[2]==2) & (x[4]==5), axis=1)], you’re calling your lambda for each of the 100000 rows in the dataframe. This has substantial overhead as a Python method call must be executed for each row.

    When you write df[(df[1]==3) & (df[2]==2) & (df[4]==5)], there’s no overhead; instead, the operation is applied to each column in a single operation, and the loop is executed in native code with the potential for vectorization (e.g. SSE).

    This isn’t exclusive to Pandas; in general, any numpy operation will be much faster if you treat arrays and matrices in aggregate instead of calling Python functions or inner loops on individual items.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

After seeing this question a few minutes ago, I wondered why the language designers
I'm seeing this exception message coming from XslCompiledTransform.Transform(), but after handling the exception the
I'm working on an editor for novel authors . After seeing programs like DarkRoom
Edited: (after seeing Luke's answer) I'm looking to develop a website and all the
I have a question about the following statement in Python if not x %
This issue came up when trying to answer this question: https://stackoverflow.com/questions/12434634 The documentation for
Ok so after reading danben's answer on this post , I guess I'm convinced
I am learning OpenGL from this tutorial . My question is about the specification
everyone. This isn't as much of an specific technical question as it is about
After reading this answer on "one file per component" approach when using WiX, I

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.