python之dict3—— 用dict 将dataframe 聚类;根据一列,对另一列进行分类/聚类

用dict 将dataframe 聚类。根据一列,对另一列进行分类/聚类

比如:对以下这个idc的dataframe, 选取各大指数成分股

python之dict3—— 用dict 将dataframe 聚类;根据一列,对另一列进行分类/聚类_第1张图片

先在dataframe中根据一列选取另一列:

 idc[idc['指数代码'] == "000300.SH"]['股票代码']

python之dict3—— 用dict 将dataframe 聚类;根据一列,对另一列进行分类/聚类_第2张图片

 然后将series可以直接放入dict的value中,或者转为list放入dict中,看需要。

stock_index_consitutesA = {}

stock_index_consitutesA['300'] = idc[idc['指数代码'] == "000300.SH"]['股票代码']

stock_index_consitutesA['500'] = idc[idc['指数代码'] == "000905.SH"]['股票代码']

stock_index_consitutesA['1000'] = idc[idc['指数代码'] == "000852.SH"]['股票代码']

stock_index_consitutesA = {}

stock_index_consitutesA['300'] = list(idc[idc['指数代码'] == "000300.SH"]['股票代码'])

stock_index_consitutesA['500'] = list(idc[idc['指数代码'] == "000905.SH"]['股票代码'])

stock_index_consitutesA['1000'] = list(idc[idc['指数代码'] == "000852.SH"]['股票代码'])

你可能感兴趣的:(python,dict,python)