I have a dataframe in which I have these column names
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?
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.
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.
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.
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'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With