开始使用免费开始使用

创建市值时间序列

现在,您可以利用股票数量,从历史价格序列中计算每个成分股在各个交易日的总市值。

该结果将作为构建市值加权股票指数的关键输入。您将在下一个练习中完成该指数的构建。

本练习是课程的一部分

Python 中的时间序列数据处理

查看课程

练习说明

我们已为您导入 pandas 为 pd,以及 matplotlib.pyplot 为 plt。我们还创建了您在上几道练习中使用过的变量 components 和 stock_prices。

  • 从 components 中选择 'Number of Shares',赋值给 no_shares,并打印结果,按默认(升序)排序。
  • 将 stock_prices 与 no_shares 相乘,创建每个股票代码的市值时间序列,并将其赋值给 market_cap。
  • 选择 market_cap 的第一行和最后一行,分别赋值给 first_value 和 last_value。
  • 使用 pd.concat() 沿着 axis=1 连接 first_value 和 last_value,并将结果绘制为水平条形图。

交互式实操练习

通过完成这段示例代码来试试这个练习。

# Select the number of shares
no_shares = ____
print(____)

# Create the series of market cap per ticker
market_cap = ____

# Select first and last market cap here
first_value = ____
last_value = ____


# Concatenate and plot first and last market cap here


编辑并运行代码