问题
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)
回答1:
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
回答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'
来源:https://stackoverflow.com/questions/47933535/replace-string-in-dataframe-if-a-condition-in-a-different-row-is-met