'Uning loc multiple times on the same column and SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame
I want to update values in the data frame several time with .loc. My code:
a['code'] = np.nan
a.loc[:,'code'] = a[a['stimulus'].str.contains("bgd4")]['response'].map(code_dict)
a.loc[:,'code'] = a[(a['stimulus'].str.contains("bearded_guy"))&
(a['syllable']==2)]['response'].map(code_dict_2)
a.loc[:,'code'] = a[(a['stimulus'].str.contains("bearded_guy"))&
(a['syllable']==1)]['response'].map(code_dict_1)
The output looks like only the last .loc works, the first two are set to NaN. And I'm getting a SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame.
How can I do this assignment correctly?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
