Search code examples
pandasdataframesklearn-pandas

How to change particular column value when defined mask is true?


I have a dataframe in which I have these column names

  • 'team1',
  • 'team2',
  • 'city',
  • 'date'.

What I want to do is to assign value of 'city' as 'dubai' when certain condition meets(which I am defining using mask).

This is what I am doing exactly:

 matches[((matches['team1']=='mi') & (matches['team2']=='rcb') & (matches['date']=='2014-04-19')),'city']='Dubai'

When all the above condition meets I want to change value in 'city'(which is null now) to 'Dubai'

The problem which arises:

'Series' objects are mutable, thus they cannot be hashed

How can I do this?


Solution

  • Bracket ([]) notation accesses the __getitem__ method of a python object (if it has a method defined). For a pd.DataFrame object, you can pass an array like object via the brackets df[array_like_object] and it will do one of a few things

    possibility 1

    # returns a copy of df with columns ['col1', 'col2']
    df[['col1', 'col2']]
    

    possibility 2

    # returns a slice of which rows have corresponding trues in the mask
    df[boolean_mask]
    

    skipping other possibilities


    You've got a boolean_mask

    ((matches['team1']=='mi') & 
     (matches['team2']=='rcb') & 
     (matches['date']=='2014-04-19'))
    

    And a column

    'city'
    

    In this case, it's perfect for loc which can process exactly that
    Per @JohnGalt

    matches.loc[
        ((matches['team1']=='mi') &
         (matches['team2']=='rcb') &
         (matches['date']=='2014-04-19')),
        'city'
    ] = 'Dubai'