0

I want to do something equivalent to

Select x,y,z from data where f(x, Y);

And f is my customized function that looks into the values of specific columns in a row and returns True or False. I tried the following:

df = df.ix[_is_detection_in_window(df['Product'], df['CreatedDate'])== True]

But I get

TypeError: 'Series' objects are mutable, thus they cannot be hashed

I think it does not iterate over the rows. I also tried:

 i = 0
   for index, row in df.iterrows():
           if _is_detection_in_window(row['Product'], row['CreatedDate']):
                   print 'in range '
                   new_df.iloc[i] = row
                   i+= 1
   df = new_df

but I get :

IndexError: single positional indexer is out-of-bounds

2 Answers 2

2

It seems like your function doesn't accept Series, but that can be changed using np.vectorize:

v = np.vectorize(_is_detection_in_window)
df = df.loc[v(df['Product'], df['CreatedDate'])]

Furthermore, you should refrain from using .ix which is now deprecated as of v20.

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

3 Comments

yes, my function accepts two strings, so I would like to iterate over the rows and select the rows that match the function f that is defined on specific columns. I've written my other try in my post, but it does not work.
Perfect man!, awesome, thanks. Can you also tell me what is wrong with my second solution.
@Alex Possibly an issue with your index. You might have wanted new_df.iloc[index] instead. Even so, if you want to filter out rows, what you're doing is incorrect.
0

Not sure how your function looks, but I assume it returns a list of bools equal to the number of rows in your df:

df = df.iloc[_is_detection_in_window(df['Product'], df['CreatedDate']), :]

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.