[英]Pandas: How to return the row from dataframe having same row values by using column value of other dataframe?
import pandas as pd
df1 = pd.DataFrame({'country': ['EUR.1M', 'EUR.1M', 'EUR.3M','EUR.3M','EUR.6M','EUR.6M','USD.3M', 'USD.3M', 'USD.3M',
'HKD.3M','HKD.3M','HKD.3M','GBP.6M', 'GBP.6M','GBP.6M'],
'inst': ['base', 'base', 'depo', 'Fut', 'Fut','base', 'irs','base', 'irs','Fut', 'irs', 'irs',
'irs','Fut', 'irs'],
'frequency' : ['Eur35Y', 'Eur40Y', 'Eur12Y','Eur10Y','Eur20Y','Eur40Y','USDIRS23Y', 'USDswap15Y','USD20Y',
'HKD50Y', 'HKD20Y','HKD10Y','GBP19Y', 'GBPIRS15Y','GBP60Y']})
df2 = pd.DataFrame({'tenor': ['Eur40Y','HKD50Y']})
I have two dataframes mentioned above and I am interested in two columns from df1 ('country'and 'frequency').我有上面提到的两个数据框,我对 df1 的两列('国家'和'频率')感兴趣。 However, am trying to pull values from column 'country' from df1 by comparing df2.但是,我试图通过比较 df2 从 df1 的“国家”列中提取值。 The objective is if any one value in df2 is present in df1['frequency'], then I dont want to pick the values from df['country'].目标是如果 df1['frequency'] 中存在 df2 中的任何一个值,那么我不想从 df['country'] 中选择值。 For example: If the 'tenor' value is similar to 'frequency' then I dont want the associated 'country' values from df1.例如:如果“男高音”值与“频率”相似,那么我不想要来自 df1 的关联“国家”值。 I am new to the pandas and picking up fast.我是 pandas 的新手,学习速度很快。 Seeking expected output as below.求预期 output 如下。 Please suggest.请建议。
list = ['EUR.3M','USD.3M','GBP.6M']
IIUC:国际大学联盟:
s = df1.loc[df1["frequency"].isin(df2["tenor"]),"country"]
print (df1.loc[~df1["country"].isin(s),"country"].unique().tolist())
#['EUR.3M', 'USD.3M', 'GBP.6M']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.