如何根据索引日期时间列的值创建新列
How can I make a new column based on values of my index datetime column
我想创建一个名为 'state' 的新专栏。根据日期时间,我想为新列赋值。所以当它介于:
A_start_646 = datetime.datetime(2022,4,27, 11,30,0)
S_start_646 = datetime.datetime(2022,4,28, 1,0,0)
我希望专栏说'A'。当它介于:
S_start_646 = datetime.datetime(2022,4,28, 1,0,0)
D_start_646 = datetime.datetime(2022,5,2, 15,25,0)
我想说 'S'。
在我的脚本(下方)中,我尝试先分别切割数据,然后再将它们加在一起。但我认为必须有更好的方法。但我真的不知道如何表达这个问题并找到答案。我希望有人能帮助我。
我有一个如下所示的数据框:
x y z bat
date
2022-04-15 10:17:14.721 0.125 0.016 1.032 NaN
2022-04-15 10:17:39.721 0.125 -0.016 1.032 NaN
2022-04-15 10:18:04.721 0.125 0.016 1.032 NaN
2022-04-15 10:18:29.721 0.125 -0.016 1.032 NaN
2022-04-15 10:18:54.721 0.125 0.016 1.032 NaN
... ... ... ...
2022-05-02 17:03:04.721 -0.750 -0.016 0.710 NaN
2022-05-02 17:03:29.721 -0.750 -0.016 0.710 NaN
2022-05-02 17:03:54.721 0.719 -0.302 -0.419 NaN
2022-05-02 17:04:19.721 -0.625 -0.048 -0.871 NaN
2022-05-02 17:04:44.721 -0.969 0.016 -0.032 NaN
这是我的代码:
data_646 = pd.read_csv('data.csv', index_col=(0), delimiter=';', skiprows=30, names = ['date','x','y','z','bat'], parse_dates=['date'])
print(data_646)
## 646
A_start_646 = datetime.datetime(2022,4,27, 11,30,0)
S_start_646 = datetime.datetime(2022,4,28, 1,0,0)
D_start_646 = datetime.datetime(2022,5,2, 15,25,0)
D_end_646 = datetime.datetime(2022,5, 2, 15,50,0)
A_646 = data_646[A_start_646 : S_start_646]
S_646 = data_646[S_start_646 : D_start_646]
D_646 = data_646[D_start_646 : D_end_646]
A_646['state']='A'
S_646['state']='S'
D_646['state']='D'
我已经找到答案了。
代码:
data_646.loc[A_start_646 : S_start_646, 'state'] = 'A'
data_646.loc[S_start_646 : D_start_646, 'state'] = 'S'
data_646.loc[D_start_646 : D_end_646, 'state'] = 'D'
我想创建一个名为 'state' 的新专栏。根据日期时间,我想为新列赋值。所以当它介于:
A_start_646 = datetime.datetime(2022,4,27, 11,30,0)
S_start_646 = datetime.datetime(2022,4,28, 1,0,0)
我希望专栏说'A'。当它介于:
S_start_646 = datetime.datetime(2022,4,28, 1,0,0)
D_start_646 = datetime.datetime(2022,5,2, 15,25,0)
我想说 'S'。
在我的脚本(下方)中,我尝试先分别切割数据,然后再将它们加在一起。但我认为必须有更好的方法。但我真的不知道如何表达这个问题并找到答案。我希望有人能帮助我。
我有一个如下所示的数据框:
x y z bat
date
2022-04-15 10:17:14.721 0.125 0.016 1.032 NaN
2022-04-15 10:17:39.721 0.125 -0.016 1.032 NaN
2022-04-15 10:18:04.721 0.125 0.016 1.032 NaN
2022-04-15 10:18:29.721 0.125 -0.016 1.032 NaN
2022-04-15 10:18:54.721 0.125 0.016 1.032 NaN
... ... ... ...
2022-05-02 17:03:04.721 -0.750 -0.016 0.710 NaN
2022-05-02 17:03:29.721 -0.750 -0.016 0.710 NaN
2022-05-02 17:03:54.721 0.719 -0.302 -0.419 NaN
2022-05-02 17:04:19.721 -0.625 -0.048 -0.871 NaN
2022-05-02 17:04:44.721 -0.969 0.016 -0.032 NaN
这是我的代码:
data_646 = pd.read_csv('data.csv', index_col=(0), delimiter=';', skiprows=30, names = ['date','x','y','z','bat'], parse_dates=['date'])
print(data_646)
## 646
A_start_646 = datetime.datetime(2022,4,27, 11,30,0)
S_start_646 = datetime.datetime(2022,4,28, 1,0,0)
D_start_646 = datetime.datetime(2022,5,2, 15,25,0)
D_end_646 = datetime.datetime(2022,5, 2, 15,50,0)
A_646 = data_646[A_start_646 : S_start_646]
S_646 = data_646[S_start_646 : D_start_646]
D_646 = data_646[D_start_646 : D_end_646]
A_646['state']='A'
S_646['state']='S'
D_646['state']='D'
我已经找到答案了。
代码:
data_646.loc[A_start_646 : S_start_646, 'state'] = 'A'
data_646.loc[S_start_646 : D_start_646, 'state'] = 'S'
data_646.loc[D_start_646 : D_end_646, 'state'] = 'D'