DavidH
DavidH

Reputation: 639

Specifying a specific date value in python pandas dataframe

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?

Upvotes: 0

Views: 2022

Answers (3)

DavidH
DavidH

Reputation: 639

The following worked:

import pandas as pd
df[(df.GROUP_ID == 'B')].ACTION_DATE = pd.to_datetime('03/31/2006 0:00')

Upvotes: 0

Venkat R
Venkat R

Reputation: 51

Try this:

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

Upvotes: 2

Kathirmani Sukumar
Kathirmani Sukumar

Reputation: 10980

Can you try this

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

At times placing the column upfront works...

Upvotes: 0

Related Questions