简体   繁体   English

Pandas:如何使用其他 dataframe 的列值从 dataframe 返回具有相同行值的行?

[英]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.

相关问题 从同一 DataFrame 中的其他行创建新的 Pandas DataFrame 列等于值 - Create New Pandas DataFrame Column Equaling Values From Other Row in Same DataFrame 如何合并来自同一行和列索引/值的两个熊猫数据框的值? - How can I merge the values from two pandas dataframe which as same row and column indexes/value? 使用一列对值进行分组,并使用 Pandas 数据框返回另一列中具有最大值的值 - Group the values using one column and return the one having max value in other column using pandas dataframe 如何使用 Pandas 中的索引列从“其他”行返回值 - How to return value from "other" row using index column in Pandas 用另一列中的相同行值替换 pandas dataframe 列中的值 - Replacing values in pandas dataframe column with same row value from another column 如何在不更改 dataframe pandas 中的其他值的情况下替换行值? - How to replace row value without changing the other values in dataframe pandas? 当同一行中的另一列为NaN时,如何从熊猫数据框中选择特定的列值? - How to select a particular column value from a pandas dataframe when another column in the same row is NaN? 如何根据列值的长度从pandas数据帧中删除一行? - How to remove a row from pandas dataframe based on the length of the column values? (行、列):值到 Pandas DataFrame - (Row, Column) : Value to Pandas DataFrame 通过将列标题设置为行值并具有值列来延长 Pandas Dataframe - Lengthening Pandas Dataframe by setting column headers as a row values and having a value column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM