简体   繁体   English

Python3 如何使用 pandas 从 dataframe 中检索特定日期以获取库存

[英]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.我想从特定日期检索 Amount 以查看股票给予的财富是多少。

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 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.如果您查看“金额”列,初始金额为 10,000 美元。

The dates range is from 2018-12-31 to 2019-12-31.日期范围为 2018 年 12 月 31 日至 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? 2019 年 3 月 5 日、2019 年 6 月 17 日、2019 年 9 月 22 日?

I want the output to be like:我希望 output 像:

Date         Amount
2019-01-07 10536.97

Please help!请帮忙!

Try this to display your desired output:试试这个来显示你想要的 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'])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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