【问题标题】:Add the Object Being Called to Table将被调用的对象添加到表中
【发布时间】:2021-11-29 21:53:54
【问题描述】:

我正在创建一个循环来为某些股票生成分析师前景数据框。

tick_list = list(df['tic'].unique())
print(tick_list)

生成:

['A', 'AAL', 'AAP', ..., 'YUM', 'ZBH', 'ZION']

从那里我使用我的循环:

import yfinance as yf
t = list(df['tic'].unique())
a = []
for i in t:
    stock = yf.Ticker(i)
    a.append(stock.recommendations)

它为列表中包含的每个对象生成一个数据框列表。但是,没有指示该表是针对哪个股票代码的。

如何将被调用的对象作为数据框中每个实例的特征包含在内?

Date Firm To Grade From Grade Action
2012-02-16 06:01:00 Deutsche Bank Buy main
2012-03-06 09:28:00 Morgan Stanley Overweight init
2012-04-19 08:35:00 Stifel Nicolaus Buy main
2012-05-15 06:14:00 Morgan Stanley Overweight main
2012-05-15 08:07:00 Stifel Nicolaus Buy main
2012-07-10 05:56:00 ISI Group Overweight init

【问题讨论】:

    标签: python pandas loops append


    【解决方案1】:

    一种方法是在包含源符号的推荐数据框中插入一个新列。

    import pandas as pd
    import yfinance as yf
    df = pd.DataFrame({'tic': ['A', 'AAL', 'AAP', 'YUM', 'ZBH', 'ZION']})
    
    t = list(df['tic'].unique())
    
    a = []
    for i in t:
        stock = yf.Ticker(i)
        tmp = stock.recommendations;
        tmp.insert(0, "Source", i); // Add new column to indicate source symbol
        a.append(tmp)
    
    print(a)
    

    【讨论】:

    • 太棒了。非常感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-30
    相关资源
    最近更新 更多