1
d =[]
e =[]
df1=DataFrame({'a':['1','2','3','4'], 'b':[2,3,4,5]})
df2=DataFrame({'a':['3','2','4','1','0'], 'c':[65,43,22,24,24]})

for i in range(len(df2)):
    for j in range(len(df1)):
        if df1.a[i]==df2.a[j]:

            d.append(df1.b[i])
            e.append(df2.c[j])

DataFrame({'star':d,'contents':e})

I'm having errors by having different length(maybe size) of dataframes in two for loops. When df1 and df2 are the same size as 4X2, it works fine, but after adding another row (5X2 for df2 and 4x2 for df1) in the dataframe I get an error saying "KeyError: 4"

Any help would be really grateful.

islhy
  • 11
  • 3

0 Answers0