如何从数据帧中提取某个值?

2024-10-05 15:25:52 发布

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

我正在废弃来自雅虎的数据,并试图使用另一个文件中的值从其数据帧中提取某个值

我已经设法刮取数据并将其显示为数据帧。问题是我正在尝试使用另一个df从数据中提取某个值

这是我得到的csv

df_earnings=pd.read_excel(r"C:Earnings to Update.xlsx",index_col=2)
stock_symbols = df_earnings.index

输出:

             Date        E Time           Company Name
Stock Symbol                                                
CALM         2019-04-01  Before The Open     Cal-Maine Foods
CTRA         2019-04-01  Before The Open      Contura Energy
NVGS         2019-04-01  Before The Open  Navigator Holdings
ANGO         2019-04-02  Before The Open       AngioDynamics
LW           2019-04-02  Before The Open         Lamb Weston`

然后我下载每个股票的csv,其中包含来自yahoo finance的数据:

driver.get(f'https://finance.yahoo.com/quote/{stock_symbol}/history?period1=0&period2=2597263000&interval=1d&filter=history&frequency=1d')

输出:

            Open     High     Low        ...       Adj Close Volume  Stock Name
Date                                     ...                                    
1996-12-12  1.81250  1.8125  1.68750     ...       0.743409  1984400        CALM
1996-12-13  1.71875  1.8125  1.65625     ...       0.777510   996800        CALM
1996-12-16  1.81250  1.8125  1.71875     ...       0.750229   122000        CALM
1996-12-17  1.75000  1.8125  1.75000     ...       0.774094   239200        CALM
1996-12-18  1.81250  1.8125  1.75000     ...       0.791151   216400        CALM

我的问题是我不知道如何从我的数据框中找到日期,并从下载的文件中提取出来

现在我不想插入这样的手动日期:

df = pd.DataFrame.from_csv(file_path)
df['Stock Name'] = stock_symbol
print(df.head())

df = df.reset_index()
print(df.loc[df['Date'] == '2019-04-01'])

输出:

     Date        Open       High   ...  Adj Close  Volume  Stock Name
5610 2019-04-01  46.700001  47.0   ...  42.987827  846900  CALM

我想要一个条件,将运行我的每只股票的数据帧和拉需要的日期

print(df.loc[df['Date'] == the date that is next to the symbol that i just downloaded the file for])

Tags: csvthe数据namedfdateindexstock
1条回答
网友
1楼 · 发布于 2024-10-05 15:25:52

我想你可以用一个变量来保存日期

for sy in stock_symbols:
    # The value from the 'Date' column in df_earnings
    dt = df_earnings.loc[df_earnings.index == sy, 'Date'][sy]

    # From the second block of your code relating to 'manual' date
    df = pd.DataFrame.from_csv(file_path)
    df['Stock Name'] = sy
    df = df.reset_index()
    print(df.loc[df['Date'] == dt])

相关问题 更多 >