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
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)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.