生活, 其他记录

上市公司市值排序的Python代码

本篇通过几行 Python 代码对上市公司的市值进行排序,运行时为获取每日的最新数据。AKShare 库的安装命令为:pip install akshare --upgrade。

这是之前的两篇:

市值排序的 Python 代码如下:

"""
This code is supported by the website: https://www.guanjihuan.com
The newest version of this code is on the web page: https://www.guanjihuan.com/archives/38193
"""

import akshare as ak
import numpy as np
stocks = ak.stock_zh_a_spot_em()
stock_data = stocks.values
new_stock_data = []
for stock in stock_data:
    if np.isnan(float(stock[9])):
        continue
    else:
        new_stock_data.append(stock)
new_stock_data = np.array(new_stock_data)
list_index = np.argsort(new_stock_data[:, 17])
list_index = list_index[::-1]
for i0 in range(30):
    stock_symbol = new_stock_data[list_index[i0], 1]
    stock_name = new_stock_data[list_index[i0], 2]
    market_capitalization = new_stock_data[list_index[i0], 17]/1e8
    print([i0+1, stock_symbol, stock_name, market_capitalization])

2024年01月28日运行的结果为(市值单位为亿元):点击展开

2024年10月22日运行的结果为(市值单位为亿元):点击展开

美股市值排序的 Python 代码如下:

"""
This code is supported by the website: https://www.guanjihuan.com
The newest version of this code is on the web page: https://www.guanjihuan.com/archives/38193
"""

import akshare as ak
import numpy as np
stocks = ak.stock_us_spot_em()
stock_data = stocks.values
new_stock_data = []
for stock in stock_data:
    if np.isnan(float(stock[9])):
        continue
    else:
        new_stock_data.append(stock)
new_stock_data = np.array(new_stock_data)
list_index = np.argsort(new_stock_data[:, 9])
list_index = list_index[::-1]
for i0 in range(30):
    stock_symbol = new_stock_data[list_index[i0], 15]
    stock_name = new_stock_data[list_index[i0], 1]
    market_capitalization = new_stock_data[list_index[i0], 9]/1e8
    print([i0+1, stock_symbol, stock_name, market_capitalization])

2024年01月28日运行的结果为(市值单位为亿美元):点击展开

2024年10月22日运行的结果为(市值单位为亿美元):点击展开

313 次浏览

【说明:本站主要是个人的一些笔记和代码分享,内容可能会不定期修改。为了使全网显示的始终是最新版本,这里的文章未经同意请勿转载。引用请注明出处:https://www.guanjihuan.com

发表评论

您的邮箱地址不会被公开。 必填项已用 * 标注

Captcha Code