Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace string in dataframe if a condition in a different row is met

Tags:

python

pandas

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)
like image 684
sacuL Avatar asked Oct 23 '25 17:10

sacuL


1 Answers

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

like image 115
tarashypka Avatar answered Oct 26 '25 05:10

tarashypka