Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 620
Pankaj Mishra Avatar asked Dec 29 '16 18:12

Pankaj Mishra


People also ask

How do you replace values in a column based on condition?

You can replace values of all or selected columns based on the condition of pandas DataFrame by using DataFrame. loc[ ] property. The loc[] is used to access a group of rows and columns by label(s) or a boolean array. It can access and can also manipulate the values of pandas DataFrame.

How do I replace specific values in a column in R?

replace() function in R Language is used to replace the values in the specified string vector x with indices given in list by those given in values. It takes on three parameters first is the list name, then the index at which the element needs to be replaced, and the third parameter is the replacement values.

How do I change a specific value in pandas?

Pandas DataFrame replace() Method The replace() method replaces the specified value with another specified value. The replace() method searches the entire DataFrame and replaces every case of the specified value.


1 Answers

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'
like image 94
piRSquared Avatar answered Oct 26 '22 04:10

piRSquared