python - 无法使用 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
解决方案
推荐阅读
- xamarin - 如何在 Android 自定义渲染器中设置字体样式
- macos - 库版本不兼容 - cdo 版本需要 11.0.0 或更高版本,但 libcurl.4.dylib 提供 7.0.0 版本
- r - 如何在ggplot中将字符用作多线图的x轴?
- php - 保持按钮禁用,直到当前页面重新加载(使用 Jquery)
- django - How to save header request using REST framework
- javascript - 当我尝试在 JSP 中加载 JSON 文件时出错
- python - Flask login_required 访问静态文件夹中的子目录
- javascript - 如何:延迟订阅弹出窗口 - jQuery
- excel - 如何在 dot net core 中使用 OpenXML 保存 .xls 文件?
- firebase - Flutter:即使应用程序关闭,如何发送推送通知?