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
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
创建一个列(具有纯零,NaN或其他内容),然后将其写在循环的末尾,即df.iloc[ind,"Date_Diff"]=diff
或具有一个列表Date_diff=[]
,然后附加在end Date_diff.append(diff)
-最后将其写入到df df["Date_diff"]=Date_diff
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.