在python中添加每周一小时的列

时间:2019-02-19 02:59:30

标签: python pandas numpy

我目前有一些时间序列数据,如下所示:

public class Point<T extends Number>{

//field values
private T xCordinate;
private T yCordinate;

//constructor
//@param x the x cordinate
//@param y the y cordinate
public Point(T x, T y){
   xCordinate = x;
   yCordinate = y;
}//end constructor

public void setX(T x){

   xCordinate = x;

}//end setX

public void setY(T y){

   yCordinate = y;

}//end setY

public T getX(){

   return xCordinate;

}//end getX

public T getY(){

   return yCordinate;

}//end getY

}//end pointlist

我要使用python添加一列“一周中的一小时”,我设法使用excel做到了这一点,但是我不确定如何在Python中做到这一点。

这就是我在excel中的解决方法:

excel

谢谢您的任何帮助。

3 个答案:

答案 0 :(得分:2)

假设样本中的日期格式为以下内容,则以下内容将通过使用时间戳生成的datetime对象生成星期几:

from datetime import datetime

timestamp = '2/19/2019 17:33'
timestamp_object = datetime.strptime(timestamp, '%m/%d/%Y %H:%M')

hour_of_week = (timestamp_object.timetuple().tm_wday * 24 + 24) - (24 - timestamp_object.timetuple().tm_hour)

print('Hour of the week: {}'.format(hour_of_week))

输出:

 Hour of the week: 41

这应该为您提供需要在上下文中应用的工具。像这样:

df['Date'] = df['Date'].apply(pd.to_datetime)

df['Hour of Week'] = ((df['Date'].dt.dayofweek) * 24 + 24) - (24 - df['Date'].dt.hour)
df['Day of Week'] = df['Date'].dt.dayofweek
df['Hour of Day'] = df['Date'].dt.hour
df = df[['Date', 'Day of Week', 'Hour of Day', 'Hour of Week', 'Price of X', 'Price of Y']]

请注意,如果您确实希望从1小时而不是0开始,则需要这样做:

df['Hour of Week'] = ((df['Date'].dt.dayofweek) * 24 + 24) - (24 - df['Date'].dt.hour) + 1

df['Hour of Day'] = df['Date'].dt.hour + 1

为避免混乱。

如下所示,将第一天偏移到1

df['Day of Week'] = df['Date'].dt.dayofweek + 1

请记住,每次进行比较时,Hour of Daydf['Date'].dt.hour都以0开头。因此,我建议使用0作为您的标准。 Day of Week也是如此。

Hour of Week开始的0中,您的数据框如下所示:

                Date  Day of Week  Hour of Day  Hour of Week  Price of X  Price of Y
0 2011-01-01 00:30:00            5            0           120          19         172
1 2011-01-01 01:00:00            5            1           121          98         105
2 2011-01-01 01:30:00            5            1           121          40          -6
3 2011-01-01 02:00:00            5            2           122          68         120
4 2011-01-01 02:30:00            5            2           122          16         182
5 2011-01-01 03:00:00            5            3           123          23         187
6 2011-01-01 03:30:00            5            3           123          94         200
7 2019-02-18 00:01:00            0            0             0          23         187
8 2019-02-18 23:15:00            0           23            23          23         187
9 2019-02-24 23:59:00            6           23           167          23         187

日期和时间增加1:

df['Hour of Week'] = ((df['Date'].dt.dayofweek) * 24 + 24) - (24 - df['Date'].dt.hour) + 1
df['Day of Week'] = df['Date'].dt.dayofweek + 1
df['Hour of Day'] = df['Date'].dt.hour + 1

您得到:

                 Date  Day of Week  Hour of Day  Hour of Week  Price of X  Price of Y
0 2011-01-01 00:30:00            6            1           121          19         172
1 2011-01-01 01:00:00            6            2           122          98         105
2 2011-01-01 01:30:00            6            2           122          40          -6
3 2011-01-01 02:00:00            6            3           123          68         120
4 2011-01-01 02:30:00            6            3           123          16         182
5 2011-01-01 03:00:00            6            4           124          23         187
6 2011-01-01 03:30:00            6            4           124          94         200
7 2019-02-18 00:01:00            1            1             1          23         187
8 2019-02-18 23:15:00            1           24            24          23         187
9 2019-02-24 23:59:00            7           24           168          23         187

答案 1 :(得分:1)

尝试一下:

假设date列的类型为datetime

dayofweek:星期几,星期一= 0,星期日= 6

df['hour_of_the_week'] = df['date'].dt.dayofweek * 24 + (df['date'].dt.hour + 1)

您可以使用以下方式将date转换为日期时间:

df['date'] = df['date'].apply(pd.to_datetime)

答案 2 :(得分:1)

如果Date列是一个datetime对象,我将使用datetime库

#+BEGIN_SRC sh :dir /sudo::
sudo /home/oo/Downloads/cuda_10.0.130_410.48_linux.run
#+END_SRC