![](/img/trans.png)
[英]How to create new column with calculation of days between date in 2 other columns in DataFrame in Pandas Python?
[英]Pandas use a date column to create a days columns with .shift
我设置了一个时间列,如下所示:
Times
9/12/2014 5:02
9/12/2014 5:03
9/12/2014 5:04
9/12/2014 5:05
9/12/2014 5:06
9/13/2014 5:07
.
.
.
9/12/2015 5:02
它从 2014 年到 2015 年,每天和一年内都有大量条目。 我想创建一个日期列,当它看到 9/12/2014 = 1 9/12/2015 = 365。我该怎么写?
我粗略地想着用 pandas.shift 来区分行之间的差异。 还有一些类似于吐出日期的内容,仅从 9/12 开始读取 12。 因此,如果 12 - 12 = 0 天列 1st entery = 1
以下是我粗略的想法。
import pandas as pd
df = pd.read_csv('OOF',index_col=0,sep=",")
j = 1
for i in len(time)
if df['Diff'] = df['time'] - df['time'].shift(1) = 0
print j the day column
else
j =+1
print j the day column
我要这个
Times days
9/12/2014 5:02 1
9/12/2014 5:03 1
9/12/2014 5:04 1
9/12/2014 5:05 1
9/12/2014 5:06 1
9/13/2014 5:07 2
. n
. n
. n
9/12/2015 5:02 366
我以前从未使用过日期框或数据转换。 所以也许我错过了一些信息。
谢谢
使用日期时间计算
df['days'] = (df['Times'] - df['Times'].min()).dt.days + 1
Times days
0 2014-09-12 05:02:00 1
1 2014-09-12 05:03:00 1
2 2014-09-12 05:04:00 1
3 2014-09-12 05:05:00 1
4 2014-09-12 05:06:00 1
5 2014-09-13 05:07:00 2
9 2015-09-12 05:02:00 366
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.