0

I would really appreciate if someone can explain why this is happening.

I am iterating over a dataframe and saving some information I am gathering online. The information doesn't save if I use df.loc[i]['column']; however, if I use df.column[i], it does! I thought both methods were equivalent, why is this happening?

This is my code:

df=pd.read_excel([ADRESS])
df.['CID']='' 

for i in range(df.shape[0]):
  info=[GETTINGDATAONLINE]
  print('all the data')
  print(info)

  if info:
      df.CID[i]=info[0]['CID']
      print('the two following should be the same')
      print(info[0]['CID'])
      print(df.CID[i])

      df.loc[i+1]['CID'] = info[0]['CID']
      print('these two should also be the same')
      print(info[0]['CID'])
      print(df.loc[i+1]['CID'])

This is what I am getting in the console:

all the data
[{'CID': 5280804, 'Synonym': .....]
the two following should be the same
5280804
5280804
these two should also be the same
5280804

Henry Ecker
  • 34,399
  • 18
  • 41
  • 57
Magdalena
  • 1
  • 3
  • `df.loc[df.index[i],'column']` - related [https://stackoverflow.com/questions/28754603/indexing-pandas-data-frames-integer-rows-named-columns](https://stackoverflow.com/questions/28754603/indexing-pandas-data-frames-integer-rows-named-columns) – wwii Sep 02 '20 at 17:02
  • Your question is why does it work that way? Maybe [https://pandas.pydata.org/docs/user_guide/indexing.html#why-does-assignment-fail-when-using-chained-indexing](https://pandas.pydata.org/docs/user_guide/indexing.html#why-does-assignment-fail-when-using-chained-indexing) – wwii Sep 02 '20 at 17:06

0 Answers0