I'm trying to modify a set of columns from a set of rows and of course I get the warning about:
A value is trying to be set on a copy of a slice from a DataFrame
I saw a similar question here but can't wrap my head around it.
So if we follow this example code:
from random import random as rd
ex= pd.DataFrame([{"group": ["a","b"][int(round(rd()))], "colA": rd()*10, "colB": rd()*10, "colC": rd()*10, "colD": rd()*10} for _ in range(20)])
cols = [col for col in ex.columns if col != "group"]
I would like to modify only the rows that belong to group a
and only on columns cols
, for which I kind of intuitively go to try (and get the caveat warning):
ex[ex["group"]=="a"][cols] = ex[ex["group"]=="a"][cols]/ex.ix[0,cols]
The number of columns match and have the same labels, so I wonder if would have to go one by one like:
for idx in ex[ex["group"]=="a"].index:
for col in cols:
ex.ix[idx, col]=ex.ix[idx, col]/ex.ix[0,col]
Which of course works, but kind of feels like a step back. So what would be the correct way of doing something like this?