首页 > 解决方案 > 无法使用 loc 保存 Pandas 数据框的信息

问题描述

如果有人能解释为什么会发生这种情况,我将不胜感激。

我正在迭代一个数据框并保存一些我在网上收集的信息。如果我使用 df.loc[i]['column']; 信息不会保存 但是,如果我使用 df.column[i],它确实如此!我认为这两种方法是等效的,为什么会这样?

这是我的代码:

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'])

这就是我在控制台中得到的:

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

标签: pythonpandasdataframepandas-loc

解决方案


推荐阅读