![](/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.