4

I have a dataframe made up of a date and a value column, kind of like this:

>>> df
         date   value
0  2016-09-10  value1
1  2016-09-10  value1
2  2016-09-10  value2
3  2016-09-10  value1
4  2016-09-12  value3
5  2016-09-12  value1
6  2016-09-13  value2
7  2016-09-13  value1

I would like to replace all of the value1 in df['value'] that fall on the date '2016-09-10' with value7. The Date column is a string series.

I looked at the documentation for pd.DataFrame.replace(), but couldn't find an argument for a conditional replace based on a separate column. Plenty of ugly ways to get this done jump to mind (for loops with .loc would do the trick), but does anyone know a nice, one-or-two line, and more pythonic way to do this? Thanks in advance!

If you want this mini-dummy-dataframe to experiment with:

import pandas as pd

data = {'date': ['2016-09-10', '2016-09-10',
                 '2016-09-10', '2016-09-10',
                 '2016-09-12', '2016-09-12',
                 '2016-09-13', '2016-09-13'],
        'value': ['value1', 'value1', 'value2', 'value1',
                  'value3', 'value1', 'value2', 'value1']}

df = pd.DataFrame(data)
0

2 Answers 2

8

How about

>> df.loc[(df['date'] == '2016-09-10') & (df['value'] == 'value1'), 'value'] = 'value7'

You may want to read Indexing and Selecting Data section and this for more info

Sign up to request clarification or add additional context in comments.

Comments

2

If you want to keep everything else same and change what you need, try this,

df.loc[df['date'] == '2016-09-10' , 'value'] = 'value7'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.