0

I am having an issue with creating a looping dataframe.

This is the current dataframe

I want the dataframe to look like this

I currently have a python list with a list of dictionaries, each dictionary is a separate 'setup' and I want each 'setup' to be a new column. My code looks something like this (I'm going to format it as best as I can!):

setups = 
[
      # THIS WOULD BE THE FIRST SET OF DATA #
  [
     {
        "Restaurant is open late": "Offered",
        "Restaurant offers a variety of meal options": "Considered",
        "Restaurant prices are affordable": "Offered",
     },
     {
        "Average_Number_of_Items_Liked": 0.8409090909090909,
        "Favorite_Percentage": 0.5293560606060606,
        "Reach": 0.6221590909090909
     },
      {
        "Restaurant prices are affordable": {
            "Claim_Favorite": {
                "Restaurant prices are affordable": 0.14772727272727273,
            },
            "Claim_Reach": {
                "Restaurant prices are affordable": 0.26988636363636365,
            },
            "Summary_Metrics": {
                "Average_Number_of_Items_Liked": 0.8409090909090909,
                "Favorite_Percentage": 0.5293560606060606,
                "Reach": 0.6221590909090909
            }
         },
        "Restaurant offers a variety of meal options": {
            "Claim_Favorite": {
                "Restaurant offers a variety of meal options": 0.038825757575757576,
            },
            "Claim_Reach": {
                "Restaurant offers a variety of meal options": 0.06060606060606061,
            },
            "Summary_Metrics": {
                "Average_Number_of_Items_Liked": 0.5710227272727273,
                "Favorite_Percentage": 0.38162878787878785,
                "Reach": 0.5198863636363636
             }
         }
     },
     [
        "Restaurant prices are affordable", "Restaurant offers a variety of meal options"
     ],
   ],

      # THIS WOULD BE THE SECOND SET OF DATA #
   [
      {
        "Restaurant is open late": "Considered",
        "Restaurant offers a variety of meal options": "Offered",
        "Restaurant prices are affordable": "Offered",
      },
      {
        "Average_Number_of_Items_Liked": 0.8409090909090909,
        "Favorite_Percentage": 0.5293560606060606,
        "Reach": 0.6221590909090909
      },
      {
        "Restaurant prices are affordable": {
            "Claim_Favorite": {
                "Restaurant prices are affordable": 0.14772727272727273,
            },
            "Claim_Reach": {
                "Restaurant prices are affordable": 0.26988636363636365,
            },
            "Summary_Metrics": {
                "Average_Number_of_Items_Liked": 0.8409090909090909,
                "Favorite_Percentage": 0.5293560606060606,
                "Reach": 0.6221590909090909
            }
         },
         "Restaurant is open late": {
            "Claim_Favorite": {
                "Restaurant is open late": 0.038825757575757576,
            },
            "Claim_Reach": {
                "Restaurant is open late": 0.06060606060606061,
            },
            "Summary_Metrics": {
                "Average_Number_of_Items_Liked": 0.5710227272727273,
                "Favorite_Percentage": 0.38162878787878785,
                "Reach": 0.5198863636363636
            }
         }
      },
      [
        "Restaurant is open late", "Restaurant prices are affordable"
      ],
   ]
]

and this is my current for loop:

lists = []
for setup in setups:
  print(setup)
  
  claims = setup[0]
  
  for i, claim in enumerate(claims):
    print(claim)
    list = []
    list.append(i + 1)
    list.append(claim)
    if claim in setup[3]:
      list.append(setup[2][claim]['Summary_Metrics']['Reach'])
    else:
      list.append(setup[0][claim])
    lists.append(list)

print(lists)

df = pd.DataFrame(lists)
df

My problem is the data is displayed as one long column, but I would like for the second set of data to become a new column almost identical to what I have with the first. I want to be able to read each set of data side by side instead of all stacked on top of one another.

E B
  • 9
  • 4
  • Use `pd.concat(lists, axis =1)` . Axis tells it to concatenate horizontally. At the end of this page in documentation: https://pandas.pydata.org/docs/reference/api/pandas.concat.html – Shivam Miglani May 24 '22 at 20:36

0 Answers0