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)
Community
  • 1
  • 1
sacuL
  • 49,704
  • 8
  • 81
  • 106

2 Answers2

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

mr.tarsa
  • 6,386
  • 3
  • 25
  • 42
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'
i.n.n.m
  • 2,936
  • 7
  • 27
  • 51