0

I've 2 dataframe, both with a column date:
I need to set in first dataframe the value of specific column found in the second dataframe,
So in first of all I find the correct row of first dataframe with:

id_row = int(dataset.loc[dataset["time"] == str(searchs.index[x])].index[0]) #example: 910

and then I want to update the value of column ['search_volume'] at this row: 910
I will do this with:

dataset['search_volume'][id_row] = searchs[kw_list[0]][x]

but I get back this error:

/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

my full code is, but not working and nothing is updated.

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]

It work fine if I test manually the update with:

dataset['search_volume'][910] = searchs[kw_list[0]][47]

What's append?!

Mariano
  • 473
  • 3
  • 12

1 Answers1

0

Use .loc:

dataset.loc[910, 'search_volume'] = searchs.loc[47, kw_list[0]]

For more info about the error message, see this

Also, there are way more efficient methods for doing this. As a rule of thumb, if you are looping over a dataframe, you are generally doing something wrong. Some potential solutions: pd.DataFrame.join, pd.merge, masking, pd.DataFrame.where, etc.

qmeeus
  • 2,341
  • 2
  • 12
  • 21