历史文章/不再维护

上市公司市值排序的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日运行的结果为(市值单位为元):

[1, '600941', '中国移动', 21692.28142661]
[2, '600519', '贵州茅台', 20567.97781896]
[3, '601398', '工商银行', 18141.07848583]
[4, '601939', '建设银行', 17100.75086004]
[5, '601857', '中国石油', 15556.78311453]
[6, '601288', '农业银行', 13509.3451075]
[7, '601988', '中国银行', 12599.79746511]
[8, '600938', '中国海油', 10883.27559954]
[9, '601628', '中国人寿', 8089.358571]
[10, '600036', '招商银行', 7848.41595103]
[11, '601318', '中国平安', 7511.72177539]
[12, '601088', '中国神华', 7112.93014389]
[13, '600028', '中国石化', 6946.1264458]
[14, '300750', '宁德时代', 6598.561854]
[15, '600900', '长江电力', 5904.18093487]
[16, '002594', '比亚迪', 5410.6501103]
[17, '601728', '中国电信', 5261.66047519]
[18, '000858', '五 粮 液', 5084.90648655]
[19, '601658', '邮储银行', 4620.90614337]
[20, '601328', '交通银行', 4433.48478071]
[21, '000333', '美的集团', 4041.23001248]
[22, '688981', '中芯国际', 3695.17392435]
[23, '300760', '迈瑞医疗', 3447.21337142]
[24, '601166', '兴业银行', 3249.09924972]
[25, '601899', '紫金矿业', 3217.10700553]
[26, '600030', '中信证券', 3087.11990448]
[27, '002415', '海康威视', 2957.80049513]
[28, '601998', '中信银行', 2952.70201703]
[29, '601138', '工业富联', 2862.39119709]
[30, '600276', '恒瑞医药', 2570.73791642]

美股市值排序的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日运行的结果为(市值单位为美元):

[1, '105.MSFT', '微软', 30021.13722553]
[2, '105.AAPL', '苹果', 29751.7802832]
[3, '107.FBGX', 'FI Enhanced Large Cap Growth ET', 24137.94058165]  
[4, '105.GOOG', '谷歌-C', 19248.3564]
[5, '105.GOOGL', '谷歌-A', 19048.1004]
[6, '105.AMZN', '亚马逊', 16443.50946844]
[7, '105.NVDA', '英伟达', 15050.2446]
[8, '105.META', 'Meta Platforms Inc-A', 10128.8569719]
[9, '106.TSM', '台积电', 6081.58929092]
[10, '106.LLY', '礼来', 6068.44651252]
[11, '105.TSLA', '特斯拉', 5825.37344901]
[12, '105.AVGO', '博通', 5640.53208777]
[13, '106.BRK_B', '伯克希尔哈撒韦-B', 5042.62791442]
[14, '106.JPM', '摩根大通', 4980.62916987]
[15, '106.V', '维萨', 4920.74409554]
[16, '106.NVO', '诺和诺德', 4816.43441467]
[17, '106.UNH', '联合健康', 4654.22407438]
[18, '106.WMT', '沃尔玛', 4422.53230392]
[19, '106.XOM', '埃克森美孚', 4116.67770822]
[20, '106.MA', '万事达', 4112.4293603]
[21, '106.JNJ', '强生', 3839.6093989]
[22, '106.PG', '宝洁', 3674.00707372]
[23, '106.HD', '家得宝', 3536.16533173]
[24, '105.ASML', '阿斯麦', 3413.7285]
[25, '106.BRK_A', '伯克希尔哈撒韦-A', 3329.160498]
[26, '106.ORCL', '甲骨文', 3151.3641808]
[27, '106.MRK', '默沙东', 3061.60669009]
[28, '105.COST', '开市客', 3047.88174382]
[29, '107.NRGU', 'MicroSectors U.S. Big Oil Index', 3044.18311415]  
[30, '106.ABBV', '艾伯维', 2902.54352012]
67 次浏览

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

发表评论

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

Captcha Code