Q1980
Q1980

Reputation: 47

How do I add the results from a calculation to a new column in a dataframe?

I have this dataframe:

    Dt1     Dt2
0   8/21/19 8/31/19
1   8/21/19 8/31/19
2   8/21/19 8/31/19
3   8/30/19 8/31/19

Then I wrote this code:

for ind in df.index:
    date_str1 = df['Dt1'][ind]
    date_str2 = df['Dt2'][ind]
    date_object1 = datetime.strptime(date_str1, " %m/%d/%y")
    date_object2 = datetime.strptime(date_str2, " %m/%d/%y")
    d = date_object2-date_object1
    diff = d.days
    print(diff)

My results were:

10
10
10
1

This is what I expect of the results, and now what I want to do is for each row in the dataframe, I want to create a new column (date_diff) and add these results to each row so in the end I have something like this:

    Dt1     Dt2      Date_diff
0   8/21/19 8/31/19  10
1   8/21/19 8/31/19  10
2   8/21/19 8/31/19  10
3   8/30/19 8/31/19  1

Upvotes: 0

Views: 80

Answers (2)

CutePoison
CutePoison

Reputation: 5355

Either create a column (with pure zeros, NaN or something else) and then write that in your loop at the end i.e df.iloc[ind,"Date_Diff"]=diff or have a list Date_diff=[] and then append in the end Date_diff.append(diff) - at last write that to your df df["Date_diff"]=Date_diff

Upvotes: 0

Quang Hoang
Quang Hoang

Reputation: 150735

You can simply do:

df['Dt1'] = pd.to_datetime(df['Dt1'])
df['Dt2'] = pd.to_datetime(df['Dt2'])

df['Date_diff'] = df['Dt2'].sub(df['Dt1']).dt.days

Upvotes: 5

Related Questions