0

I have the dataframe with columns of strings. I want to use a part of it in some function. A part to be used is defined by condition which passed into function.

def myfunc(condition):
    tmp_df = df[condition].copy()

    #doing something else wit tmp_df

    return some_result

My questions are:

  1. How can I pass condition for string columns? For example:

    df.str_column.str.len()>10

  2. How to pass an empty condition, when I need to use all dataframe?

I found solutions for numeric columns, but how to work with strings?

2
  • How dynamic... if you already have code that you'd like to use such as df.str_column.str.len() > 10 - can you not just pass a dataframe that's already subsetted to myfunc... so you call it as myfunc(df[df.str_column.str.len() > 10]) ? Commented Aug 27, 2018 at 11:06
  • I want to do some code with different parts of dataframe, based on different conditions. You're right! It would be easier to pass a part of dataframe. Commented Aug 27, 2018 at 11:42

1 Answer 1

1

I believe is necessary create boolean masks:

#boolean mask
cond = df.str_column.str.len()>10
#return all values
cond = [True] * len(df)

myfunc(cond)
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.