![](/img/trans.png)
[英]how to add value to a new column to a dataframe based on the match of another dataframe?
[英]How to add a column that based on the different value for every row conducts excel type “INDEX, MATCH, MATCH” search from another dataframe?
我正在尝试添加一个名为“ Yield to Worst”的列,并且该行针对该行使用“ Ticker”值,并对另一个数据框进行INDEX,MATCH,MATCH来查找特定股票行情的值Yield to Worst 。 数据框位于“股票行情指示器”和“ Funds_Data”下方。 以及所需的输出。
行情数据帧
Index Tickers
0 IEF US Equity
1 JNK US Equity
2 HYG US Equity
3 LQD US Equity
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
期望的输出
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
尝试输入的代码
for ticker in range (0, len(Tickers)):
Yield = pd.DataFrame(funds_data.loc['Yield to Worst', ticker])
Tickers['Yield'] = Yield
谢谢你的帮助
您可以在Yield
行funds_data
tickers
与funds_data
合并:
tickers.merge(funds_data.loc['Yield'],
how='left', left_on='Tickers', right_index=True)
输出:
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:您的示例funds_data
没有“ MBB US Equity”的数据,因此以NaN
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.