I'm trying to set a value in a multi-index dataframe. I am using .loc but I am still getting the SettingsWithCopyWarning. I have read the documentation but I'm not sure how I should be doing this. What am I doing wrong?
"SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead
See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy self.obj[item] = s"
arrays = [np.array(['A', 'A', 'B', 'B', 'C', 'C', 'D', 'D']),
np.array(['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight'])]
df = pd.DataFrame(np.random.randn(6, 8), columns=arrays)
mask = df.loc[:,('A','one')].ge(0)
df[mask].loc[:,('B')] = "A"
The SettingWithCopyWarning occurs because you are doing .loc[]
on a subset of the DataFrame. Use .loc[]
to select the subset directly and you won't get this error.
arrays = [np.array(['A', 'A', 'B', 'B', 'C', 'C', 'D', 'D']),
np.array(['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight'])]
df = pd.DataFrame(np.random.randn(6, 8), columns=arrays)
mask = df.loc[:,('A','one')].ge(0)
df.loc[mask,('B')] = "A"
You can also simplify the line where you calculate the mask, since you don't need .loc[]
for this.
mask = df[('A','one')].ge(0) # or, df[('A','one')] >= 0
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.