简体   繁体   中英

Python3 How to retrieve specific dates from a dataframe for stock using pandas

I want to retrieve the Amount from specific dates to see what is the wealth that the stock has given.

From what I have done so far:

INPUT

initial_amount = 10000
amount_A = []

for numbers in A['Return_A']:
    amount_A.append(initial_amount * (1 + numbers))

df = pd.DataFrame({'Stock Price A': A['Adj Close'],
                   'Stock Returns A': A['Return_A'],
                   'Amount': amount_A
                  })

df['Amount'] = df['Stock Returns A'].add(1).fillna(1).cumprod()*initial_amount

print(df.head())

OUTPUT

             Stock Price A  Stock Returns A           Amount
Date                                                   
2018-12-31      161.670441            NaN       10000.000000
2019-01-02      166.490067       0.029811       10298.114228
2019-01-03      164.051193      -0.014649       10147.259607
2019-01-04      169.412827       0.032683       10478.899286
2019-01-07      170.351578       0.005541       10536.965017

If you take a look at the "Amount" column, the initial amount is $10,000.

The dates range is from 2018-12-31 to 2019-12-31.

How do I retrieve the values of "Amount" from specific dates such as; for eg:

5 March 2019, 17 June 2019, 22 September 2019?

I want the output to be like:

Date         Amount
2019-01-07 10536.97

Please help!

Try this to display your desired output:

df[df['Date']=='2019-01-07'][['Date','Amount']]

If the date is in your index, try this:

df[df.index=='2019-01-07'][['Amount']]

To round your numbers to the nearest dollar:

df['Amount'] = round(df['Amount'])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM