Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pulling specific value from Pandas DataReader dataframe?

Here is the code I am running:

def competitor_stock_data_report():
    import datetime
    import pandas_datareader.data as web

    date_time = datetime.datetime.now()
    date = date_time.date()

    stocklist = ['LAZ','AMG','BEN','LM','EVR','GHL','HLI','MC','PJT','MS','GS','JPM','AB']
    start = datetime.datetime(date.year-1, date.month, date.day-1)
    end = datetime.datetime(date.year, date.month, date.day-1)

    for x in stocklist:
        df = web.DataReader(x, 'google', start, end)
        print(df)
        print(df.loc[df['Date'] == start]['Close'].values)

The problem is in the last line. How do I pull the specific value of the date specified 'Close' value?

             Open   High    Low  Close   Volume
Date                                           
2016-08-02  35.22  35.25  33.66  33.75   861111
2016-08-03  33.57  34.72  33.42  34.25   921401
2016-08-04  33.89  34.22  33.77  34.07   587016
2016-08-05  34.55  34.94  34.31  34.35   463317
2016-08-08  34.54  34.75  34.31  34.74   958230
2016-08-09  34.68  35.12  34.64  34.87   732959

I would like to get 33.75 for example, but the date is dynamically changing..

Any suggestions?

like image 231
sgerbhctim Avatar asked Jul 14 '26 10:07

sgerbhctim


2 Answers

IMO the easiest way to get a column's value in the first row:

In [40]: df
Out[40]:
              Open    High     Low   Close   Volume
Date
2016-08-03  767.18  773.21  766.82  773.18  1287421
2016-08-04  772.22  774.07  768.80  771.61  1140254
2016-08-05  773.78  783.04  772.34  782.22  1801205
2016-08-08  782.00  782.63  778.09  781.76  1107857
2016-08-09  781.10  788.94  780.57  784.26  1318894
...            ...     ...     ...     ...      ...
2017-07-27  951.78  951.78  920.00  934.09  3212996
2017-07-28  929.40  943.83  927.50  941.53  1846351
2017-07-31  941.89  943.59  926.04  930.50  1970095
2017-08-01  932.38  937.45  929.26  930.83  1277734
2017-08-02  928.61  932.60  916.68  930.39  1824448

[252 rows x 5 columns]

In [41]: df.iat[0, df.columns.get_loc('Close')]
Out[41]: 773.17999999999995

Last row:

In [42]: df.iat[-1, df.columns.get_loc('Close')]
Out[42]: 930.38999999999999
like image 62
MaxU - stop WAR against UA Avatar answered Jul 15 '26 22:07

MaxU - stop WAR against UA


Recommended

  • df.at[df.index[-1], 'Close']
  • df.iat[-1, df.columns.get_loc('Close')]
  • df.loc[df.index[-1], 'Close']
  • df.iloc[-1, df.columns.get_loc('Close')]

Not intended as public api, but works

  • df.get_value(df.index[-1], 'Close')
  • df.get_value(-1, df.columns.get_loc('Close'), takeable=True)

Not recommended, chained indexing
There could be more, but do I really need to add them

  • df.iloc[-1].at['Close']
  • df.loc[:, 'Close'].iat[-1]

All yield

34.869999999999997
like image 39
piRSquared Avatar answered Jul 15 '26 22:07

piRSquared



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!