add a new column named age to the trader information by computing the age of each trader by using their birthday against the assignment due date (2024-04-28). in python

main.py
import pandas as pd

# sample trader information data
data = {'trader_id': [1, 2, 3],
        'name': ['Alice', 'Bob', 'Charlie'],
        'birthday': ['1990-05-15', '1985-11-20', '1993-02-10']}

df = pd.DataFrame(data)

# convert birthday column to datetime
df['birthday'] = pd.to_datetime(df['birthday'])

# define the assignment due date
due_date = pd.Timestamp('2024-04-28')

# calculate age by subtracting the birthday from the assignment due date
df['age'] = (due_date - df['birthday']).astype('<m8[Y]')

print(df)
521 chars
20 lines

This code adds a new column 'age' to the trader information dataframe by computing the age of each trader based on their birthday against the assignment due date (2024-04-28).

related categories

gistlibby LogSnag