1

This expression

df["column"].diff() != 0

Gives me a pandas series of booleans. I now would like a column df["result"] where there's the value 100 for every True value in df["column"] and 0 for every False.

I don't understand why this doesn't work:

df["result"] = 100 if df["column"].diff() != 0 else 0

I understand I'd have to use loc, but from this:

df.loc[df["column"].diff() != 0]

How do I then set the result column?

2 Answers 2

3

Here is best use numpy.where for set 2 values by condition, solution is vectorized:

df["result"] = np.where(df["column"].diff() != 0, 100, 0)

Your code:

df["result"] = 100 if df["column"].diff() != 0 else 0

not working, because here are used 1d arrays, (Series) so cannot be used scalar solution.

You also get:

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

And it means there is no clear, what is scalar output from array of boolean from df["column"].diff() != 0.

More information is in Using if/truth statements with pandas

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

Comments

1

May be you are looking for something like this:

df.loc[df['column'].diff()!=0, 'Result'] = 100

Edit after jezrael's comment:

df['diff'] = df['column'].diff().fillna(0)
df.loc[df['diff'] != 0, 'Result'] = 100

2 Comments

get NaNs instaed 0, so if want use this solution, first need set 0 to column
I think he meant the result column would need to be set to 0 first. Thanks for your input!

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.