0

I have following example of data frame

  Marked       City
1   0        New York
2   1         California

I want to select rows from data frame, where Marked is 0 and apply some function to City row. The data frame is big (more than 1M row) and function to apply is complex, so the option to use condition statement to whole data frame is bad, since i still iterate whole 1M records. Selection of rows in data frame based on condition reduces the number rows to 300k and applying function to them is much faster

0

1 Answer 1

1

Use loc access:

def some_func(x): return 1

mask = df.Marked == 0

df.loc[mask, 'new_col'] = df.loc[mask].apply(some_func, axis=1)
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your answer! I tried something like this and got TypeError: 'Series' objects are mutable, thus they cannot be hashed

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.