0

I'm new in python, so.... I have a dataframe like this:

    id   city      name     text
    1    Boston    Rosie    I have some text here, as you can see.
    2    New York  Liza     I love my cat

So I'l like to search inside each row the text and have some result like:

I rearch in the text "love" or "love" && "cat" and I want return the city or the name.

I tried the follow code:

   if df[df['text'].str.contains("love") | df['text'].str.contains("cat")]:
    print(df['name'])

It's throwing an error of the form "The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()."

AMC
  • 2,642
  • 7
  • 13
  • 35
HugoB
  • 121
  • 7
  • When you say not working, do you mean an error message is popping up? (if yes, what does the error message say). Or is it running fine, just not producing the desired results? – Nebulous29 Jan 22 '20 at 23:42
  • @Nebulous29 this is the error: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all(). – HugoB Jan 22 '20 at 23:45
  • Does this answer your question? [Truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()](https://stackoverflow.com/questions/36921951/truth-value-of-a-series-is-ambiguous-use-a-empty-a-bool-a-item-a-any-o) – AMC Jan 23 '20 at 00:29
  • @HugoB Always include the entire error message. It would have made it easier to tell that there already answers to this question, for one. – AMC Jan 23 '20 at 00:29

1 Answers1

2

Use boolean index with pandas.Series.str.contains:

df['name'][df['text'].str.contains("cat|love")]

Output:

1    Liza
Name: name, dtype: object
Chris
  • 29,127
  • 3
  • 28
  • 51
  • you user 'or' here but if I use 'and' (&), it gives me the empty result. Why? – HugoB Jan 23 '20 at 15:50
  • And if I'd like to add also the 'city' in my result? – HugoB Jan 23 '20 at 15:53
  • @HugoB Because there is no operator for AND in regex. In that case, you will have to use it `df['name'][df['text'].str.contains("cat") & df['text'].str.contains("love")]`. For the second comment, try `df[['name','city']][...]` – Chris Jan 23 '20 at 23:45