1

I need to change the value of myCol if a condition is true. If the condition is not true, nothing should happen (if I do else None, it writes None as value to myCol)

tmp_df = someDataframe.groupby('ID').myCol.apply(lambda x: 'a' if (x=='A').any() else *DO NOTHING, THE myCol VALUE HAS TO STAY SAME*)

tmp_df=tmp_df.to_frame()
1
  • GroupBy expects a value for column myCol, wither it needs to be 'a' or something! Commented May 10, 2019 at 15:37

3 Answers 3

2

If you want to leave the value unchanged you can set the else value to the lambda argument x:

df.col.apply(lambda x: new_value if some_condition else x)

In your language:

tmp_df = someDataframe.groupby('ID').myCol.apply(lambda x: 'a' if (x=='A').any() else x)
Sign up to request clarification or add additional context in comments.

2 Comments

this does an Attribute Error
Can you supply a small sample dataframe that replicates this error? It's not causing an error in the test I used, but maybe I'm not understanding your use case. Also, is this Python 2 or 3?
0

Depending on the complexity there is probably a more efficient answer here. I use np.where to get your answer.

 import numpy as np
 df['conditional'] = np.where(
      (df['conditional'] == 'A'), # Condition
      'a', # Value if true
      df['conditional'] # Value if false
      # /\ this is equal to the original value so it has no effect
 )

Documentation: https://docs.scipy.org/doc/numpy/reference/generated/numpy.where.html

2 Comments

While the other answers may yield the same result, this will be more efficient for a large dataset with simply if statements. It may not matter with fewer than 1,000 records, but when you are in the millions it will certainly make a difference.
I think this won't work because I use a groupBy either than one row
0

did you try this:

tmp_df = someDataframe.groupby('ID').myCol.apply(lambda x: 'a' if (x=='A').any() else x)

5 Comments

Attribute Error
what is the type of myCol values?
tmp_df = someDataframe.groupby('ID').myCol.apply(lambda x: 'a' if (x=='A').any() else str(x) ))
so I think you want to do something like: tmp_df = someDataframe.groupby('ID').myCol.apply(lambda x: x.replace('A','a'))
str(x) takes '[values of the other columns], dtype: object'

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.