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

2024-07-04 05:08:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图添加一个名为“yieldtoworst”的列,它为每一行使用该行的“Ticker”值,并对另一个数据帧进行索引、匹配、匹配,以找到特定Ticker的yieldtoworst值。数据帧位于“Tickers”和“Funds\u Data”下面。所需的输出。你知道吗

股票数据帧

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

基金数据数据帧

         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

谢谢你的帮助


Tags: 数据indexustickerfundsyieldtickersmth
1条回答
网友
1楼 · 发布于 2024-07-04 05:08:27

可以将tickersfunds_data合并到Yield行:

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

另外,在你的样本funds_data中没有“MBB美国股票”的数据,所以它的结果是NaN

相关问题 更多 >

    热门问题