如何将计算结果添加到数据框中的新列?

时间:2019-11-26 16:14:40

标签: python pandas dataframe jupyter-notebook jupyter

我有这个数据框:

    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

然后我写了这段代码:

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)

我的结果是:

10
10
10
1

这是我对结果的期望,现在我要对数据帧中的每一行进行操作,我想创建一个新列(date_diff)并将这些结果添加到每一行,因此最后我有了像这样的东西:

    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

2 个答案:

答案 0 :(得分:5)

您可以简单地做到:

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

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

答案 1 :(得分:0)

要么创建一列(包含纯零,NaN或其他内容),然后将其写在循环的末尾,即df.iloc[ind,"Date_Diff"]=diff或拥有列表Date_diff=[],然后附加在末尾{{ 1}}-最后将其写入您的df Date_diff.append(diff)