[Solved] Add a new column with the list of values from all rows meeting a criterion


Something like this should work…

df = pd.DataFrame({'date': ['2017-01-01 01:01:01', '2017-01-02 01:01:01', '2017-01-03 01:01:01', '2017-01-30 01:01:01', '2017-01-31 01:01:01'],
                   'value': [99,98,97,95,94]})

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

def get_list(row):

   subset = df[(row['date'] - df['date'] <= pd.to_timedelta('5 days')) & (row['date'] - df['date'] >= pd.to_timedelta('0 days'))]
   return str(subset['value'].tolist())

df['list'] = df.apply(get_list, axis=1)

Output:

              date  value          list
0 2017-01-01 01:01:01     99          [99]
1 2017-01-02 01:01:01     98      [99, 98]
2 2017-01-03 01:01:01     97  [99, 98, 97]
3 2017-01-30 01:01:01     95          [95]
4 2017-01-31 01:01:01     94      [95, 94]

2

solved Add a new column with the list of values from all rows meeting a criterion