In Python, how to loop through a list, append to pandas df, do an api lookup based on the list, and more

First off, thank you for taking the time to review my question. I have looked up how to do this but have not found a solution.

I have a list of values (here I am using string which are the names of shapes as a simple example). I would like to loop through this list and use the names of the Shapes to lookup the Length and Width from an API(not important which one), then save the Shape name, the length, and the width to a DataFrame, and then calculate a ratio between the length and the width and create a third column “ratio” – in the DataFrame- from the calculation.

I have the list, that would go in column 1, the Length(column 2) and Width(column 3) would be coming from an arbitrary API, and the ratio(column 3) would come from a calculation As an example:

#create/initialize the dataframe
    Shapes = pd.DataFrame({
        'Shape': [],
        'Length': [],
        'Width': [],
        'Ratio_Length2Width':[]
    })
    
    #Here is the shape list
    Shape = ["Rectangle", "Square", "Circle"]
      
    for i in Shape:
        #HELP here I would have code to add the Shape I am currently on to dataframe column 1 "shape"
        length = shape.length("Shape")
        #HELP here I would have code to add this to the data frame
        width = shape.width("Shape")
        #HELP here I would have code to add this to the data frame
        ratio = length/width
        #HELP here I would have code to add this to the data frame    
    
#The intended result
    print(Shapes)

       Shape          Length   Width    Ratio
    0    Rectangle      L1       W1      R1
    1    Square         L2       W2      R2
    2    Circle         L3       W3      R3

Answer

You can do this,

import pandas as pd
#create/initialize the dataframe
Shapes = pd.DataFrame({
    'Shape': [],
    'Length': [],
    'Width': [],
    'Ratio_Length2Width':[]
})

#Here is the shape list
Shape = ["Rectangle", "Square", "Circle"]

length = 10
width = 5

for i in range(len(Shape)):
    Shapes.loc[i] = {'Shape':Shape[i],'Length':length,
                     'Width':width,'Ratio_Length2Width':length/width}


Alternatively, you can also create multiple lists that grow, create a dictionary and convert that into a dataframe like this,

import pandas as pd

#Here is the shape list
Shape = ["Rectangle", "Square", "Circle"]

length = 10
width = 5

lengthlist = []
widthlist = []
ratiolist = []

for i in range(len(Shape)):
    lengthlist.append(length)
    widthlist.append(width)
    ratiolist.append(length/width)

Shapes = pd.DataFrame.from_dict({'Shape':Shape,'Length':lengthlist,
                                 'Width':widthlist,'Ratio_Length2Width':ratiolist})

I created a table which looks like this in both instances.

        Shape       Length  Width   Ratio_Length2Width
0       Rectangle   10.0    5.0     2.0
1       Square      10.0    5.0     2.0
2       Circle      10.0    5.0     2.0