首先,我将结果存储在一些列表中。我想将它们更改为pandas数据帧,以便我可以进行合并和操作等。
我遇到过这个
Convert Python dict into a dataframe 我可以使用pd.Series将字典转换为数据框。
所以我创建了三个列表。它们是一个包含空字典的列表,我想存储转换后的列表。包含索引名称的列表。以及包含存储结果的列表的列表。
它不会更新空字典a,b,c。
这是我创建的空数据框。
df_result_total_ys = pd.DataFrame()
df_result_total_rng = pd.DataFrame()
df_result_age1534_ys = pd.DataFrame()
df_result_age1534_rng = pd.DataFrame()
df_result_age3554_ys = pd.DataFrame()
df_result_age3554_rng = pd.DataFrame()
df_result_age55_ys = pd.DataFrame()
df_result_age55_rng = pd.DataFrame()
df_result_childbel12_ys = pd.DataFrame()
df_result_childbel12_rng = pd.DataFrame()
df_result_childabv12_ys = pd.DataFrame()
df_result_childabv12_rng = pd.DataFrame()
df_result_nochild_ys = pd.DataFrame()
df_result_nochild_rng = pd.DataFrame()
df_result_income1_ys = pd.DataFrame()
df_result_income1_rng = pd.DataFrame()
df_result_income2_ys = pd.DataFrame()
df_result_income2_rng = pd.DataFrame()
df_result_income3_ys = pd.DataFrame()
df_result_income3_rng = pd.DataFrame()
这是我创建的三个列表。
lst_result = [result_total_ys,
result_total_rng,
result_age1534_ys,
result_age1534_rng,
result_age3554_ys,
result_age3554_rng,
result_age55_ys,
result_age55_rng,
result_childbel12_ys,
result_childbel12_rng,
result_childabv12_ys,
result_childabv12_rng,
result_nochild_ys,
result_nochild_rng,
result_income1_ys,
result_income1_rng,
result_income2_ys,
result_income2_rng,
result_income3_ys,
result_income3_rng]
lst_df = [df_result_total_ys,
df_result_total_rng,
df_result_age1534_ys,
df_result_age1534_rng,
df_result_age3554_ys,
df_result_age3554_rng,
df_result_age55_ys,
df_result_age55_rng,
df_result_childbel12_ys,
df_result_childbel12_rng,
df_result_childabv12_ys,
df_result_childabv12_rng,
df_result_nochild_ys,
df_result_nochild_rng,
df_result_income1_ys,
df_result_income1_rng,
df_result_income2_ys,
df_result_income2_rng,
df_result_income3_ys,
df_result_income3_rng]
lst_name = ['Ranking based on % yes','Ranking based on highest % as 1st choice',
'Ranking based on % yes age 15-34','Ranking based on highest % as 1st choice age 15-34',
'Ranking based on % yes age 35-54','Ranking based on highest % as 1st choice age 35-54',
'Ranking based on % yes age above 55','Ranking based on highest % as 1st choice above 55',
'Ranking based on % yes With Children Aged 12 or Below','Ranking based on highest % as 1st choice With Children Aged 12 or Below',
'Ranking based on % yes With Children Aged Over 12','Ranking based on highest % as 1st choice With Children Aged Over 12',
'Ranking based on % yes with No Children','Ranking based on highest % as 1st choice with No Children',
'Ranking based on % yes With income below 25000','Ranking based on highest % as 1st choice With income below 25000',
'Ranking based on % yes With income between 25000 and 44999','Ranking based on highest % as 1st choice With income between 25000 and 44999',
'Ranking based on % yes with income above 45000','Ranking based on highest % as 1st choice income above 45000']
这是将dict转换为dataframe
的代码for df, name, result in zip(lst_df, lst_name, lst_result):
s = pd.Series(result, name=name)
s.index.name = 'Enhancement options'
df = s.reset_index()