Attribute Error Problems

HI, I am fairly new to Anvil. I am trying to get a simple mockup where in I pass a string to my colab notebook and I get a result back. I am getting a problem saying that there is an AttributeError: ‘DataFrame’ object has no attribute ‘post_likes_count’. I have attached my code. Can you please help.

@anvil.server.callable

def predict_iris(instusername):

  df= pd.read_excel('/content/SampleData_sample.xlsx')

  influsername = instusername 

  df1=df[df.influencer_username == influsername]

  df1['response']= 'Null'

  for index, row in df1.iterrows():

        hashtag_list = []

        regex = "#(\w+)"

        hashtag_list = re.findall(regex,df1.post_text[index])   

        df1.response[index]= hashtag_list

  avg_likes= df1.post_likes_count.mean()

  df2= df1.explode('response')

  df2['tag']= 1

  tabl1 = pd.pivot_table(df2, values=['tag', 'post_likes_count'], index=['response'], aggfunc={'post_likes_count': np.mean, 'tag': np.sum})

  df2_anl= tabl1.reset_index()

  df2_anl['response1'] = df2_anl.response.str.lower()

  ## error is coming up on the line below 
  df2_anl.score = ((df2_anl.post_likes_count)/(avg_likes))*df2_anl.tag

  df2_anl['score'] = df2_anl.tag

  df2_inter= df2_anl.sort_values(by=['score'], ascending = False).head()

  df3 = pd.DataFrame(columns = ['hash1', 'hash2', 'hash3', 'hash4', 'hash5'])

  if(df2_inter.shape[0]>4): 

        df3= df3.append({'hash1' : df2_inter['response'].iloc[0], 'hash2' : df2_inter['response'].iloc[1], 'hash3' : df2_inter['response'].iloc[2], 'hash4' : df2_inter['response'].iloc[3], 'hash5' : df2_inter['response'].iloc[4]}, ignore_index = True)

  elif(df2_inter.shape[0]==4):

        df3= df3.append({'hash1' : df2_inter['response'].iloc[0], 'hash2' : df2_inter['response'].iloc[1], 'hash3' : df2_inter['response'].iloc[2], 'hash4' : df2_inter['response'].iloc[3]}, ignore_index = True)

  elif(df2_inter.shape[0]==3): 

        df3= df3.append({'hash1' : df2_inter['response'].iloc[0], 'hash2' : df2_inter['response'].iloc[1], 'hash3' : df2_inter['response'].iloc[2]}, ignore_index = True)

  elif(df2_inter.shape[0]==2): 

        df3= df3.append({'hash1' : df2_inter['response'].iloc[0], 'hash2' : df2_inter['response'].iloc[1]}, ignore_index = True)

  elif(df2_inter.shape[0]==1):

        df3= df3_br.append({'hash1' : df2_inter['response'].iloc[0]}, ignore_index = True)

  else:

        df3

  sty = df3.hash1[0] + " " + df3.hash2[0] + " " + df3.hash3[0] + " " + df3.hash4[0] + " " + df3.hash5[0]

  return sty

Can someone please help with the above? Thanks

The error literally means the dataframe object above, df1 does not contain an object variable called post_likes_count

Is it supposed to be a column? I do not use pandas that often but I don’t think the columns become attribute names automatically? I could be wrong.

later you are referring to this as some type of column with a string in values=[] , so it could be the df1.post_likes_count.mean() code is incorrect somehow?

when i use this same code as a seperate function it works fine. there is a column named post_likes_count but when i put it in the anvil function it throws error which i am not sure why

This suggests that the copy of the file

being read on the Anvil Server is not the copy you are reading elsewhere. Perhaps the Anvil Server copy does not have the desired column?

quite possible. data is being run on colab cloud and not on anvil server. any suggestions for this situation?

How about this?

  1. Get the then-current file contents from your colab source on a timely basis.
  2. Pass those contents (as a Media Object) to the Anvil Server when needed.