【发布时间】:2021-07-14 10:05:22
【问题描述】:
我有 2 个数据框,都带有日期列:
我需要在第一个数据框中设置在第二个数据框中找到的特定列的值,
所以首先我找到了第一个数据帧的正确行:
id_row = int(dataset.loc[dataset["time"] == str(searchs.index[x])].index[0]) #example: 910
然后我想在这一行更新['search_volume'] 列的值:910
我会这样做:
dataset['search_volume'][id_row] = searchs[kw_list[0]][x]
但我得到了这个错误:
/root/anaconda3/lib/python3.7/site-packages/ipykernel_launcher.py:8: SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame
See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
我的完整代码是,但没有工作,没有任何更新。
for x in range(len(searchs)):
id_row = int(dataset.loc[dataset["time"] == str(searchs.index[x])].index[0])
dataset['search_volume'][id_row] = searchs[kw_list[0]][x]
如果我手动测试更新,它工作正常:
dataset['search_volume'][910] = searchs[kw_list[0]][47]
什么附加?!
【问题讨论】:
标签: python-3.x pandas dataframe