以下内容引用自GPT,有用望采纳:
可以使用Pandas中的MultiIndex方法建立三维数组。例如,首先通过pd.date_range()
方法生成时间序列,再通过pd.MultiIndex.from_product()
方法生成MultiIndex对象作为DataFrame的索引,其中第一层是行业名称,第二层是时间。然后填充DataFrame中的其他信息即可。
下面是可能的实现方法:
import pandas as pd
IndustryTotal = w.wset("sectorconstituent", "date=2023-05-25;sectorid=a39901012d000000", usedf=True)[1]
secname = IndustryTotal.wind_code
start = '2010-03-31'
end = '2023-03-31'
Time = pd.date_range(start=start, end=end, freq='q')
# 使用MultiIndex建立三维空DataFrame
idx = pd.MultiIndex.from_product([secname, Time], names=['Sector', 'Date'])
SectorInfo = pd.DataFrame(index=idx, columns=['Capital_Cost', 'Operate_Income', 'Profitmargin'])
for sector in secname:
for date in Time:
res = w.wss(sector, "cash_pay_acq_const_fiolta,oper_rev,grossprofitmargin", \
"unit=1;rptDate={};rptType=1".format(date.strftime('%Y-%m-%d')), usedf=True)[1]
SectorInfo['Capital_Cost'][(sector, date)] = res['CASH_PAY_ACQ_CONST_FIOLTA'][0]
SectorInfo['Operate_Income'][(sector, date)] = res['OPER_REV'][0]
SectorInfo['Profitmargin'][(sector, date)] = res['GROSSPROFITMARGIN'][0]
# 将MultiIndex转化为列,有需要时可直接使用reset_index方法还原
SectorInfo = SectorInfo.stack().to_frame().reset_index()
SectorInfo.columns = ['Sector', 'Date', 'Indicator', 'Value']
# 添加百分比信息
SectorInfo['Shifted'] = SectorInfo.sort_values(by='Date').groupby('Sector')['Value'].shift(-1)
SectorInfo['Percentage'] = (SectorInfo['Value'] - SectorInfo['Shifted']) / SectorInfo['Shifted']
SectorInfo.dropna(subset=['Shifted'], inplace=True)
SectorPercent = SectorInfo.groupby(['Date', 'Indicator'])[['Percentage']].apply(lambda x: \
pd.Series(stats.percentileofscore(x['Percentage'].tolist(), x[x['Sector']=='886017.WI']['Percentage'].values[0]), \
index=['Percentile'])).reset_index()
SectorPercent
希望这能满足您的需求。