Python Pandas读取Excel并提取股票信息
import pandas as pd
import openpyxl
file_name = '02-林业.xlsx'
wb = openpyxl.load_workbook(file_name, read_only=True)
name_lst = wb.sheetnames
stock_name = name_lst[0]
df_stock_values = pd.read_excel(file_name, sheet_name=stock_name)
lst_date = list(df_stock_values['trade_date'])
lst_open = list(df_stock_values['open'])
lst_close = list(df_stock_values['close'])
lst_high = list(df_stock_values['high'])
lst_low = list(df_stock_values['low'])
# 请编写函数,功能为,分别输出上市公司的名字列表和股票代码列表
# name_sh为上市公司名称和代码的列表,其形式为: ['平潭发展000592.SZ', 'ST景谷600265.SH']
def get_company_info():
name_sh = []
for name in name_lst:
if name.startswith('ST'):
sh = 'SH'
else:
sh = 'SZ'
code = name.split('.')[0]
company_name = df_stock_values.loc[df_stock_values['ts_code']==code+'.SH', 'name'].values[0]
name_sh.append(company_name+code+'.'+sh)
return name_sh
# 调用函数,输出上市公司名称和代码的列表
print(get_company_info())
原文地址: https://www.cveoy.top/t/topic/nj2T 著作权归作者所有。请勿转载和采集!