Search code examples
pythonpandasdataframepandas-loc

Assigning a new value to a dataframe cell using df.loc


Can someone please tell me what the right syntax would be to do the following:

dfpollution3.loc(max(dfpollution3.index),'newhospinextday') = 10

I get the following error:

    dfpollution3.loc(max(dfpollution3.index),'newhospinextday') = 10
    ^
SyntaxError: cannot assign to function call

Solution

  • The issue is that you are using the incorrect brackets, and you should use at not loc in this case.

    dfpollution3.at[max(dfpollution3.index),'newhospinextday'] = 10

    Indexing is done with square brackets [], and function calls are with parentheses ().