PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, May 10, 2022

[FIXED] How to sum the products of a pandas dataframe?

 May 10, 2022     pandas, product, python     No comments   

Issue

New to python trying to code a product summation formula that would iterate through a pandas dataframe. Any tips? Sorry if this question has already been asked.

result = []
i = 0
while i < len(r):
    np.prod([1*(1+r)])
    result.append(r[i])
    i += 1
print(result)

This is the code I came up with if anybody has a better way of doing please let me know. r is my pandas dataframe. When I run this code all I get is infinite and it's not close to the formula I actually like to put that data through

The formula I'm trying to implement is

the formula I'm trying to implement


Solution

Based on your equation use DataFrame.apply with numpy.prod

# Example dataframe
df = pd.DataFrame()
df['A'] = [1, 2, 3]
df['B'] = [4, 5, 6]

result = (df + 1).apply(lambda x: np.prod(x))
result

A     24
B    210
dtype: int64


Answered By - ResidentSleeper
Answer Checked By - Marie Seifert (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing