0

First of all, I have results stored in some lists. I want to change them to pandas dataframe so that I can do merge and operations etc.

I have come across this

Convert Python dict into a dataframe that I can covert dictionary to dataframe by using pd.Series.

So I created three lists. They are a list containing empty dictionary which I want to store the converted list. A list containing the name of the index. And a list containing the list where the results are stored.

It won't update the empty dictionary a, b, c.

This is the empty dataframe I created.

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()

This is the three lists I created.

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']

And this is the code to convert dict to 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()
JOHN
  • 1,411
  • 3
  • 21
  • 41

0 Answers0