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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T01:08:58+00:00 2026-06-19T01:08:58+00:00

I am writing a function that computes conditional probability all columns in a pd.DataFrame

  • 0

I am writing a function that computes conditional probability all columns in a pd.DataFrame that has ~800 columns. I wrote a few versions of the function and found a very big difference in compute time over two primary options:

col_sums = data.sum()   #Simple Column Sum over 800 x 800 DataFrame

Option #1:
{‘col_sums’ and ‘data’ are a Series and DataFrame respectively}

[This is contained within a loop over index1 and index2 to get all combinations]

joint_occurance = data[index1] * data[index2]
sum_joint_occurance = joint_occurance.sum()
max_single_occurance = max(col_sum[index1], col_sum[index2])
cond_prob = sum_joint_occurance / max_single_occurance #Symmetric Conditional Prob
results[index1][index2] = cond_prob

Vs.

Option #2: [While looping over index1 and index2 to get get all combinations]
Only Difference is instead of using DataFrame I exported the data_matrix to a np.array prior to looping

new_data = data.T.as_matrix() [Type: np.array]

Option #1 Runtime is ~1700 sec
Option #2 Runtime is ~122 sec

Questions:

  1. Is converting the contents of DataFrames to np.array’s best for computational tasks?
  2. Is the .sum() routine in pandas significantly different to to .sum() routine in NumPy or is the difference in speed due to the label access to data?
  3. Why are these runtimes so different?
  • 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-19T01:09:00+00:00Added an answer on June 19, 2026 at 1:09 am

    While reading the documentation I came across:

    Section 7.1.1 Fast scalar value getting and setting Since indexing with [] must handle a lot of cases (single-label access, slicing,
    boolean indexing, etc.), it has a bit of overhead in order to figure
    out what you’re asking for. If you only want to access a scalar value,
    the fastest way is to use the get_value method, which is implemented
    on all of the data structures:

    In [656]: s.get_value(dates[5])
    Out[656]: -0.67368970808837059
    In [657]: df.get_value(dates[5], ’A’)
    Out[657]: -0.67368970808837059
    

    Best Guess:
    Because I am accessing individual data elements many times from the dataframe (order of ~640,000 per matrix). I think the speed reduction came from how I referenced the data (i.e. “indexing with [] handles a lot of cases”) and therefore I should be using the get_value() method for accessing scalars similar to a matrix lookup.

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

Sidebar

Related Questions

I am writing a Function that has a return-type which will dynamically change depending
I am writing a function in c++ that computes the sum of double numbers
I am writing function that involve other function from base R with a lot
I'm writing a function that replaces long hex coded color ( #334455 ) with
Suppose I'm writing a function that takes a list of integers and returns only
I am writing a function that takes in a output target file and a
I am writing a function that takes a string, string pointer and an int.
I am writing a function that takes in an argument. From that argument, I
I'm writing a function that returns the first n ( n <= strlen(src) )
How would i go about writing a function that would handle the data 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.