Home > Enterprise >  Adding a list of different length under a certain condition to a date time index pandas dataframe
Adding a list of different length under a certain condition to a date time index pandas dataframe

Time:05-02

How can I insert a list of values in a certain position of a dataframe with date time index under the condition that I want to insert the list after a certain value is > a certain number? Example below:

import pandas as pd
example_list = [2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5]
example_index =  pd.date_range('2022-01-01', periods=120, freq='1min')
example_df = pd.DataFrame({'example values': np.arange(120)})
example_df.index = example_index
example_df

Output:

                    example values
2022-01-01 00:00:00 0
2022-01-01 00:01:00 1
2022-01-01 00:02:00 2
2022-01-01 00:03:00 3
2022-01-01 00:04:00 4
... ...
2022-01-01 01:55:00 115
2022-01-01 01:56:00 116
2022-01-01 01:57:00 117
2022-01-01 01:58:00 118
2022-01-01 01:59:00 119

I want to insert the example_list as a new column called "example_values_2" at the position where the example_values>20. Is this possible?

CodePudding user response:

IIUC, you can find the index of this value and slice:

start = example_df['example values'].gt(20).argmax()
idx = example_df.index
example_df.loc[idx[start:start len(example_list)], 'example_values_2'] = example_list

output:

                     example values  example_values_2
                ...             ...               ...
2022-01-01 00:20:00              20               NaN
2022-01-01 00:21:00              21               2.0
2022-01-01 00:22:00              22               2.0
2022-01-01 00:23:00              23               2.0
2022-01-01 00:24:00              24               2.0
2022-01-01 00:25:00              25               3.0
2022-01-01 00:26:00              26               3.0
2022-01-01 00:27:00              27               3.0
2022-01-01 00:28:00              28               3.0
2022-01-01 00:29:00              29               4.0
2022-01-01 00:30:00              30               4.0
2022-01-01 00:31:00              31               4.0
2022-01-01 00:32:00              32               4.0
2022-01-01 00:33:00              33               5.0
2022-01-01 00:34:00              34               5.0
2022-01-01 00:35:00              35               5.0
2022-01-01 00:36:00              36               NaN
                ...             ...               ...
  • Related