0

I have a pandas DataFrame with three columns:

x = [4,2,4,-4,-80,1]
y = [1,2,3,-10,50,1]
z = [3,4,-1,-3,0,-1]
df = pd.DataFrame({'x': x,'y': y, 'z':z})

And I need to create a function that returns the maximum value reached by the accumulative max function. In order to do this I try:

def get_max(DataFrame):
    DataFrame['CumSum'] = DataFrame.cumsum()
    print(max(DataFrame['CumSum']))

If I apply this function for the first time, by writing for example:

get_max(df['x'] )

it works correctly and it returns 10, but if I try to apply it a second time on df['x'] I get an error: ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

Not sure about the error, I think that perhaps the problem occurs because I am trying to create a new column that already has been created thru this line:

DataFrame['CumSum'] = DataFrame.cumsum()

I would appreciate if someone could tell me if there is any way of solving this.

Thanks a lot in advance.

1 Answer 1

1

You haven't created a new column through this line:

DataFrame['CumSum'] = DataFrame.cumsum()

If you change your function to:

def get_max(Series):
    print(max(Series.cumsum()))

It will work on a column of the data, as you want it to

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.