如何计算csv文件中给定月份和天数之间的天数(2020年1月至2020年3月)?

时间:2020-09-30 01:33:33

标签: python-3.x pandas date datetime

如果我想计算从2020年1月1日到2020年3月31日的3个月之间的天数,并将结果保存在新列中,最好的方法是它吗?

示例:-

2020年1月1日应为 1 ,2020年3月31日应为 91
因此 2 为2020年1月2日
2020年2月1日将是 32
2,2020年3月将是 62

在下面的文件中呈现前20行,它们具有完全相同的column_names: 因此,从1月1日到3月31日共有91天,我希望在列名计数日中使用,因此对于1月1日,可以从天列和月列中捕获,其中月份1 = jan,2 = 2月,3 =行进和天数与此相关,因此通过使用这两列,我希望在列名countdays中计算天数

Code which selects columns that I want from the csv file.
y = df[['month','day','Weekdays']]
y.head(20)
OUTPUT:
   month    day Weekdays
0   1       24   4.0
1   3       13   4.0
2   2       23   6.0
3   1       19   6.0
4   1       18   5.0
5   2        8   5.0
6   2        8   5.0
7   3       11   2.0
8   2        9   6.0
9   3        6   4.0
10  1       31   4.0
11  2       21   4.0
12  3       11   2.0
13  1       31   4.0
14  2       21   4.0
15  3       11   2.0
16  1       23   3.0
17  1       18   5.0
18  2        4   1.0
19  1       14   1.0

尝试生成一个新列,使用代码为我提供天数:

 for m in y['month'].unique():
for d in y[y['month']==m]['day'].unique():
    y.loc[(y['month']==m) &(y['day']==d),'countdays']=datetime.datetime(2020, m, d).weekday() - datetime.datetime(2020, m, d).weekday()
OUTPUT:
         month  day Weekdays countdays
0           1   24     4.0      0.0
1           3   13     4.0      0.0
2           2   23     6.0      0.0
3           1   19     6.0      0.0
4           1   18     5.0      0.0
...       ...   ...    ...      ...
40134409    1   13     0.0      0.0
40134410    2   18     1.0      0.0
40134411    2   20     3.0      0.0
40134412    2   3      0.0      0.0
40134413    3   3      1.0      0.0
ACTUAL GOAL:
         month  day Weekdays countdays
0           1   24     4.0      24
1           3   13     4.0      74
2           2   23     6.0      54

and so on for all the rows.

在countdays列中,我想要在示例中提到的那些数字。因此,对于索引0的计数日应显示 24 ,索引1的计数应显示 73 ,而索引2的计数日应显示 54

这可能会有所帮助:(1,Jan = 1和31,Jan = 31),(1,Feb = 32和29,Feb = 60),(1,Mar = 61和31,Mar = 91)

因此,1月1-31日应该是1-31天,2月1-29日应该是32-60天,3月1-31日应该是61-91天。

如果问题不清楚,请告诉我,我正在尽力向您解释,非常感谢您的提前帮助。 :) (如果不清楚,请随时进行任何编辑或更改)

1 个答案:

答案 0 :(得分:1)

使用to_datetime作为日期时间,然后使用Series.dt.dayofyear

df['new'] = pd.to_datetime(df[['month','day']].assign(year=2020)).dt.dayofyear

print (df)
    month  day  Weekdays  new
0       1   24       4.0   24
1       3   13       4.0   73
2       2   23       6.0   54
3       1   19       6.0   19
4       1   18       5.0   18
5       2    8       5.0   39
6       2    8       5.0   39
7       3   11       2.0   71
8       2    9       6.0   40
9       3    6       4.0   66
10      1   31       4.0   31
11      2   21       4.0   52
12      3   11       2.0   71
13      1   31       4.0   31
14      2   21       4.0   52
15      3   11       2.0   71
16      1   23       3.0   23
17      1   18       5.0   18
18      2    4       1.0   35
19      1   14       1.0   14