0

I have a dataframe df, with two columns, GROUP_ID={A,B} and ACTION_DATE. What I want to do is to replace the ACTION_DATE value to 03/31/2006, if the GROUP_ID's value is B. Data type of ACTION_DATE is datetime64[ns].

So, I tried the following.

df[(df.GROUP_ID == 'B')].ACTION_DATE = '03/31/2006 0:00'

The above line runs with no errors, but the resulting dataframe remains unchanged.

Could someone point out what I am missing?

Brian
  • 14,610
  • 7
  • 35
  • 43
DavidH
  • 619
  • 2
  • 8
  • 15

3 Answers3

2

Try this:

df.loc[df.GROUP_ID == 'B', 'ACTION_DATE'] = '03/31/2006 0:00'
isherwood
  • 58,414
  • 16
  • 114
  • 157
Venkat R
  • 51
  • 3
  • Thanks. I tried your suggestion, and I got this error message --- AttributeError: 'str' object has no attribute 'view' – DavidH May 04 '15 at 15:41
0

Can you try this

df['ACTION_DATE'][df['GROUP_ID'] == 'B'] = '03/31/2006 0:00'

At times placing the column upfront works...

Kathirmani Sukumar
  • 10,445
  • 5
  • 33
  • 34
0

The following worked:

import pandas as pd
df[(df.GROUP_ID == 'B')].ACTION_DATE = pd.to_datetime('03/31/2006 0:00')
DavidH
  • 619
  • 2
  • 8
  • 15