简体   繁体   English

如何添加基于每一行的不同值的列,以从另一个数据框中进行excel类型“ INDEX,MATCH,MATCH”搜索?

[英]How to add a column that based on the different value for every row conducts excel type “INDEX, MATCH, MATCH” search from another dataframe?

I am trying to add a column called "Yield to Worst" and that for every row it use the "Ticker" value for that row and conducts a INDEX, MATCH, MATCH to another dataframe to find the value Yield to Worst for a particular ticker. 我正在尝试添加一个名为“ Yield to Worst”的列,并且该行针对该行使用“ Ticker”值,并对另一个数据框进行INDEX,MATCH,MATCH来查找特定股票行情的值Yield to Worst 。 The dataframes are below "Tickers" and "Funds_Data". 数据框位于“股票行情指示器”和“ Funds_Data”下方。 The desired output as well. 以及所需的输出。

Tickers Dataframe 行情数据帧

Index   Tickers
0   IEF US Equity
1   JNK US Equity
2   HYG US Equity
3   LQD US Equity

Funds_Data Dataframe Funds_Data数据

         JNK US Equity  HYG US Equity   LQD US Equity   IEF US Equity
AUM      9560           16313           31525           13169
Duration 3.6            3.3             8.8             7.4
1-Mth    1.17           0.94            0.85            0.11
3-Mth    4.11           3.59            3.38            1.93
YTD      9.52           8.66            6.61            2.21
Yield    6.46           6.23            4.08            2.49

Desired Output 期望的输出

Index   Tickers    Yield
0   IEF US Equity   2.49
1   JNK US Equity   6.46
2   HYG US Equity   6.23
3   LQD US Equity   4.08

Attempted Code 尝试输入的代码

for ticker in range (0, len(Tickers)):
    Yield = pd.DataFrame(funds_data.loc['Yield to Worst', ticker])
Tickers['Yield'] = Yield

Thank you for all the help 谢谢你的帮助

You can merge tickers with funds_data taking the Yield row: 您可以在Yieldfunds_data tickersfunds_data合并:

tickers.merge(funds_data.loc['Yield'],
              how='left', left_on='Tickers', right_index=True)

Output: 输出:

             Tickers  Yield
Index                      
0      JNK US Equity   6.46
1      HYG US Equity   6.23
2      LQD US Equity   4.08
3      IEF US Equity   2.49
5      MBB US Equity    NaN

PS There's no data for "MBB US Equity" in your sample funds_data , so it comes out as NaN PS:您的示例funds_data没有“ MBB US Equity”的数据,因此以NaN

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何根据另一个 dataframe 的匹配为 dataframe 的新列添加值? - how to add value to a new column to a dataframe based on the match of another dataframe? 如何根据 Row_id 列将值写入 dataframe 的另一列并且匹配列中存在值? - How to write the values to another column of dataframe based on Row_id column and value exist in match column? 根据熊猫中的行匹配,用另一个DataFrame中的值有条件地填充列 - Conditionally fill column with value from another DataFrame based on row match in Pandas 如何将基于列的 dataframe 中的值添加到基于行的另一个 dataframe 中? - How do I add the value from one dataframe based on a column to another dataframe based on a row? 检查数据框列是否包含来自另一个数据框列的整数并根据匹配添加列 - Check if dataframe column contains an integer from another dataframe column and add column based on match Python 根据另一个 dataframe 中的列值匹配列名 - Python match a column name based on a column value in another dataframe 如何根据另一个宽数据框中的匹配和范围值在一个长数据框中添加一列? - How to add a column in one long dataframe based on a match and range values in another wide dataframe? Python Pandas DataFrame - 如何根据另一列(日期类型)中的部分匹配对 1 列中的值求和? - Python Pandas DataFrame - How to sum values in 1 column based on partial match in another column (date type)? 当一行中某一列的值与另一行另一列中的值匹配时,如何匹配pyspark数据框中的两行? - How can I match two rows in a pyspark dataframe when the value in a column in a row matches the value in another column in another row? 向 dataframe 添加一个新列,其中每一行根据它来自的 dataframe 的标题采用不同的值 - Add a new column to a dataframe in which each row adopts a different value based on the title of the dataframe it came from
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM