[英]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: 您可以在
Yield
行funds_data
tickers
与funds_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.