【代码报错】如何优化代码运行速度?
由bqnvfzc5创建,最终由small_q 被浏览 21 用户
怎么提升速度?
import dai
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
pd.set_option('display.max_rows', None)
sd = '2023-12-01'
ed = '2024-01-02'
data_com = dai.query("SELECT date, instrument, industry_level1_code, industry_level1_name FROM cn_stock_industry_component WHERE industry='sw2021'", filters={'date': [sd, ed]}).df()
data_pe = dai.query('select date, instrument, pe_ttm from cn_stock_valuation', filters={'date': [sd, ed]}).df()
result = []
for (date, industry_level1_code, industry_level1_name), group in data_com.groupby(['date', 'industry_level1_code', 'industry_level1_name']):
ins_com = group['instrument'].unique().tolist()
pe_com = data_pe[(data_pe['date']==date) & (data_pe['instrument'].isin(ins_com))]
df = pd.merge(group, pe_com, how='left', on=['date', 'instrument'])
result.append(pd.DataFrame({
'date': date,
'industry_level1_code': industry_level1_code,
'industry_level1_name': industry_level1_name,
'pe_ttm': df['pe_ttm'].mean()
}, index=[0]))
df = pd.concat(result, axis=0)
df
filtered_df = df[df['industry_level1_name']=='银行']
filtered_df
plt.figure(figsize=(20, 6))
plt.plot(filtered_df['date'], filtered_df['pe_ttm'], marker='o', color='b', linestyle='-')
plt.title('历史市盈率')
plt.show()
这我输出的银行的历史市盈率,但是没要运算好久,怎么样才可以提升运算速度
\