[英]How to filter a string in a column of Pandas data frame using for loop
[英]How to use dynamic string to filter data frame using Python Pandas
數據幀
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
下面的代碼可以正常工作,並給出索引為0和3的輸出
df_mismatched = df_common[ (df_common['MARKET_x'] != df_common['MARKET_y']) | (df_common['CLUSTER_x'] != df_common['CLUSTER_y']) ]
我們如何動態建立這樣的過濾條件? 類似於下面的代碼,因此下次無需進行硬編碼
str_common = '(df_common["MARKET_x"] != df_common["MARKET_y"]) | (df_common["CLUSTER_x"] != df_common["CLUSTER_y"])'
df_mismatched = df_common[str_common]
為了動態起見,您可以在python中使用query
,例如:
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
請記住,如果列名稱包含空格或特殊字符,則無法產生正確的結果。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.