13

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?

zx485
  • 28,498
  • 28
  • 50
  • 59
Pankaj Mishra
  • 550
  • 6
  • 18
  • Use `.loc` like `matches.loc[((matches['team1']=='mi') & (matches['team2']=='rcb') & (matches['date']=='2014-04-19')),'city'] = 'Dubai'` – Zero Dec 29 '16 at 18:30
  • Thank you sir @JohnGalt Can you explain little bit the difference between the two ? I am getting confused.Thanks anyways – Pankaj Mishra Dec 29 '16 at 18:39
  • Read about [Indexing and Selecting Data](http://pandas.pydata.org/pandas-docs/stable/indexing.html) in pandas. The access methods `pd[ix]` and `pd.col` rely on Numpy indexing and Python attributes, and carry the limitation of those. The pandas-specific access methods such as `.loc[ix, col]` are to be preferred. –  Dec 29 '16 at 19:03

1 Answers1

19

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'
piRSquared
  • 285,575
  • 57
  • 475
  • 624