简体   繁体   中英

How to use dynamic string to filter data frame using Python Pandas

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]

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM