could someone please explain this behavior to me? this is my dataframe:
df = pd.DataFrame(
{
'key1' : ['f1', 'f1', 'f2', 'f2', 'f3'],
'key2' : ['fm1', 'fm2', 'fm1', 'fm2', 'fm1'],
'k' : np.random.rand(5),
'c' : [100, 200, 100, 150, 400],
})
df.set_index(["key1","key2"],inplace=True)
when I try to change a value:
df.loc["f1","fm1"].loc["k"]=-999
nothing happens. and df.loc["f1","fm1"].loc["k"]
still gives the old value
I am wondering what I am doing wrong, and how should I achieve this?
CodePudding user response:
Use a tuple and only one loc
:
>>> df.loc[("f1", "fm1"), "k"] = -999
>>> df
k c
key1 key2
f1 fm1 -999.000000 100
fm2 0.082072 200
f2 fm1 0.119572 100
fm2 0.752586 150
f3 fm1 0.947604 400
>>>
The reason your code doesn't work, is because df.loc["f1", "fm1"]
references to an object that has no interference with the original df
, it returns a completely new dataframe and replaces the k
value, but it doesn't replace it in df
as well.
Read more about it here on the pandas documentation.
CodePudding user response:
Because you're trying to change the value on a copy of a slice from the dataframe. Read about it here: Why does assignment fail when using chained indexing?
The desired result can be achieved using below code instead:
df["k"].loc["f1", "fm1"] = -999