0

I have a dateframe that looks like:

 |Launch       |Delivery   |Step-up
0|2020-04-22   |102        |NaT
1|2020-09-02   |140        |2021-01-03
2|2019-12-24   |90         |2020-04-20
3|2020-06-14   |nan        |2022-02-18
 ...

i want to do some calculations with those columns to create a new column called maturity.

if there is nan in the delivery then the maturity for that row should also be nan.

if there is no step-up then maturity = Launch + Delivery.

if step-up does exist and it is < launch + Delivery, then maturity = step-up.

else the maturity is launch + delivery.

so ideally the dataframe should look like:

 |Launch       |Delivery   |Step-up      |Maturity
0|2020-04-22   |10         |NaT          |2020-05-02
1|2020-09-02   |14         |2020-09-10   |2020-09-10
2|2019-12-24   |9          |2020-01-20   |2020-01-02
3|2020-06-14   |nan        |2020-07-18   |nan
...

1 Answer 1

1

You just need to iterate throw your dataframe, create a new dataframe and merge them.

Preliminaries

import pandas as pd
import datetime

data = {'Launch':['2020-04-22', '2020-09-02', '2019-12-24', '2020-06-14'],
        'Delivery':['10', '14', '9', 'nan'],
        'Step-up':['NaT', '2021-01-03', '2020-04-20', '2022-02-18']}

df = pd.DataFrame(data)

Here the section that answers your question:

# create a new dataframe
append = {'Maturity':[]}

# iterate throw all rows of the old dataframe
for index, row in df.iterrows():
    # for each row make your computation
    if row['Delivery'] == 'nan':
        # append your data to the new dataframe
        append['Maturity'].append('nan')
    elif row['Step-up'] == 'NaT':
        append['Maturity'].append(datetime.datetime.strptime(row['Launch'], '%Y-%m-%d') + datetime.timedelta(days=int(row['Delivery'])))
    elif row['Step-up'] != 'NaT':
        launch_plus_delivery = datetime.datetime.strptime(row['Launch'], '%Y-%m-%d') + datetime.timedelta(days=int(row['Delivery']))
        stepup = datetime.datetime.strptime(row['Step-up'], '%Y-%m-%d')
        if stepup < launch_plus_delivery:
            append['Maturity'].append(row['Step-up'])
        else:
            append['Maturity'].append(launch_plus_delivery)

# add your new data as a new column to the old dataframe
df['Maturity'] = append['Maturity']
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.