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!
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)
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
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