2

I have pandas dataframe with the following contents:

    A   B     C  D
0 red, t1, blue, 1
1 red, t1, yellow, 2
2 red, t0, green, 1
3 red, t0, blue, 1
4 red, t0, blue, 2
5 green, t1, yeallow, 1
6 green, t0, red, 1
7 green, t0, yellow, 1
8 blue, t0, yellow, 1
9 blue, t1, red, 1
10 yellow, t1, red, 3

How to extract all the rows that for A equal to 'green' AND for B equal to t0 for example?

minerals
  • 6,090
  • 17
  • 62
  • 107
  • `df[df.A.str.contains("green") & df.B.str.contains("t0")]` since you used the word "contain". If you want exact matching, `df[(df.A == "green") & (df.B == "t0")]` – ayhan Mar 28 '16 at 16:30
  • Please add this as an answer. – minerals Mar 28 '16 at 16:32
  • Did you check this? http://stackoverflow.com/questions/26640129 – Gurupad Hegde Mar 28 '16 at 16:34
  • I do not like how the question is formulated in the link you provided. So, I think it is better to have a separate question with an example and a short answer, as given by @ayhan – minerals Mar 28 '16 at 16:35
  • This will not detect the colors in column C, but you can adjust the approach as follows. Define `set_of_needed_words`. Write `df = df[df.apply(lambda s: s.isin(set_of_desired_words))]`. – hilberts_drinking_problem Mar 28 '16 at 16:36
  • @minerals It is not a problem to be a dupe. Now this post will serve as a signal to the other, Not something to worry about. Cheers :) – Bhargav Rao Mar 28 '16 at 18:53

1 Answers1

2

As answered by @ayhan,

Partial string match:

df[df.A.str.contains("green") & df.B.str.contains("t0")]

Exact string match:

df[(df.A == "green") & (df.B == "t0")]
minerals
  • 6,090
  • 17
  • 62
  • 107