Search code examples
pythonpandasdataframepandas-groupbysklearn-pandas

if column years is >=10, user personal details should be replaced with his id (pandas)


I am new to pandas.

Here I am iterating through each row and checking the exit date of a user if his exit date is >= 10 his personal details should be replaced with his id.

I am stuck please help.

for edate in pd.to_datetime(df1['EXIT_DATE']):

    rdelt = relativedelta(datetime.today(),edate)

    df1['years'] = rdelt.years

    # its modifying each row in a DataFrame.
    #df1.loc[flag,['first_name','middel_name','email']] = df1['user_id'] 

Solution

  • You can try the code below to avoid the loop:

    # Ensure EXIT_DATE dtype is a datetime64
    df1['EXIT_DATE'] = pd.to_datetime(df['EXIT_DATE'])
    
    df1['years'] = pd.Timestamp.today().year - df1['EXIT_DATE'].dt.year
    df1.loc[df1['years'] >= 10, ['first_name','middle_name','email']] = df['user_id']