Home > Blockchain >  Pandas new column based on list of indexes
Pandas new column based on list of indexes

Time:03-20

This is my DataFrame

Date, Value
2022-01-01 00:00:00,1.1377
2022-01-01 00:05:00,1.1376
2022-01-01 00:10:00,1.1377
2022-01-01 00:15:00,1.13775
2022-01-01 00:20:00,1.13759
2022-01-01 00:25:00,1.13777
2022-01-01 00:30:00,1.137
2022-01-01 00:35:00,1.137776
2022-01-01 00:40:00,1.13775
2022-01-01 00:45:00,1.13781
2022-01-01 00:50:00,1.13783
2022-01-01 00:55:00,1.13690
2022-01-01 01:00:00,1.13674
2022-01-03 01:00:00,1.137
2022-01-03 01:05:00,1.13687
2022-01-03 01:10:00,1.13708
..........

It contains about 15000 values. I find extremum highs in it and get them as list of indexes:

l = [120, 502, 1147 ........... and so on]

Now I want new column 'extremum' with True or False values. If this index is extremum, value is True, otherwise False or n/a. Sorry, I don't know pandas syntaxis well. How can I get this result?

I try to do something like this:

df['extremum'] = [index is in list l, True, False],

but I don't know how to write it.

CodePudding user response:

try this:

import numpy as np
df['extremum'] = np.where(df.index.isin(l),True,False)
  • Related