1

I have a dataframe in python, want to replace Fri as this friday and the rest of the rows in that colume as NULL enter image description here

Use this code will replace all the rows with this friday, which is not what i want

import datetime
today = datetime.date.today()
friday = today + datetime.timedelta( (4-today.weekday()) % 7 )
this_firday = friday.strftime('%Y-%m-%d')

df['date3'] = df.loc[(df['date'] == 'Fri'), 'date'] = this_firday 
df

my expected result is

enter image description here

0

1 Answer 1

1

try via map():

df['date']=df['date'].map({'Fri':this_firday})

OR

via loc:

df.loc[(df['date'] == 'Fri'), 'date'] = this_firday
df.loc[(df['date'] != 'Fri'),'date']=float('NaN')

OR

you can also use np.where():

#import numpy as np
df['date']=np.where((df['date'] == 'Fri'),this_firday,np.nan)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.