I have a pandas dataframe. I am trying to modify name
column value in the last row
I try
df.loc[-1,'name'] = "something"
this works
Now I filter few rows from the df
with a query and call it df_query
and my last row in df_query
is
id name
21 965 kris
I check the index -1
df_query.loc['name'].iloc[-1]
it shows "kris"
now on df_query
i try
df_query.loc[-1,'name'] = "something"
it adds an extra row instead of replacing kris
with something
id name
21 965.0 kris
-1 NaN "something"
also convers id into float
from int
why sometimes it works and sometimes it doesnt
later after searching i found at https://stackoverflow.com/a/49510469
Just using iloc[-1, 'a] won't work as -1 is not in the index.
I couldnt understand the reason given above
and says to try:
df_query.loc[df_query.loc.index[-1],'name'] = "something"
and now it works.
Can someone explain whats happening