Home > Blockchain >  How can I make a new column based on values of my index datetime column
How can I make a new column based on values of my index datetime column

Time:06-04

I would like to make a new column named 'state'. And based on the datetime I would like to give a value to the new column. So when it is between:

A_start_646 = datetime.datetime(2022,4,27, 11,30,0)
S_start_646 = datetime.datetime(2022,4,28, 1,0,0)

I would like the column to say 'A'. And when it is between:

S_start_646 = datetime.datetime(2022,4,28, 1,0,0)
D_start_646 = datetime.datetime(2022,5,2,   15,25,0)

I would like it to say 'S'.

In my script (below) I tried to first cut the data separately first to add them together after. But I think there must be a better way. But I really don't know how to express this question and find the answer. I hope someone can help me out.

I have a dataframe that looks like this:

                           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

And this is my code:

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'

CodePudding user response:

I already found the answer.

code:

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'
  • Related