1

DataFrame

    PROJECT  CLUSTER_x  MARKET_x  CLUSTER_y  MARKET_y     Exist
0   P17      A          CHINA     C          CHINA        both
1   P18      P          INDIA     P          INDIA        both
2   P16      P          AMERICA   P          AMERICA      both
3   P19      P          INDIA     P          JAPAN        both

This below code works perfectly alright and gives output as index 0 and 3

df_mismatched = df_common[ (df_common['MARKET_x'] != df_common['MARKET_y']) | (df_common['CLUSTER_x'] != df_common['CLUSTER_y']) ]

How we can dynamlically build such filter criteria? something like below code, so that next time hardcoding won't be necessary

str_common = '(df_common["MARKET_x"] != df_common["MARKET_y"]) | (df_common["CLUSTER_x"] != df_common["CLUSTER_y"])'
df_mismatched = df_common[str_common]
3
  • Maybe something like query?, like con = "(MARKET_x!=MARKET_y)|(CLUSTER_x!=CLUSTER_y)" then df.query(con). Commented Oct 12, 2018 at 9:02
  • 1
    @SandeepKadapa - working nice, create answer ;) Commented Oct 12, 2018 at 9:08
  • 1
    it worked..perfect. @SandeepKadapa Thanks a lot Commented Oct 12, 2018 at 9:09

1 Answer 1

2

For the dynamic purpose, you can use query in python like:

con = "(MARKET_x!=MARKET_y)|(CLUSTER_x!=CLUSTER_y)"
print(df.query(con))

  PROJECT CLUSTER_x MARKET_x CLUSTER_y MARKET_y Exist
0     P17         A    CHINA         C    CHINA  both
3     P18         P    INDIA         P    JAPAN  both

Remember that if the columns names have spaces or special characters it fails to produce the right results.

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.