1

I have a table such as follow (in black). I would like to create an additional column such as visible in red, to the right. Basically for each slice variable in the "slice" column, I want retrieve the corresponding values from the "low" column.

enter image description here

I have tried countless ways of doing that, I already posted a question here trying to address one of the approach i though would work:

Python Pandas: Function doesn't work when used with apply()

One of the respondents to that question suggested that i simply ask how to achieve it, hence here I am.

I also repost below the code to reconstruct that dataframe:

df = pd.DataFrame(dict, columns=["low", "slices"])

  {'low': {0: 1207.25,
  1: 1207.5,
  2: 1205.75,
  3: 1206.0,
  4: 1201.0,
  5: 1202.75,
  6: 1203.75},
 'slices': {0: [slice(1, 1, None)],
  1: [slice(1, 2, None), slice(2, 2, None)],
  2: [slice(1, 3, None), slice(2, 3, None), slice(3, 3, None)],
  3: [slice(1, 4, None),
   slice(2, 4, None),
   slice(3, 4, None),
   slice(4, 4, None)],
  4: [slice(1, 5, None),
   slice(2, 5, None),
   slice(3, 5, None),
   slice(4, 5, None),
   slice(5, 5, None)],
  5: [slice(1, 6, None),
   slice(2, 6, None),
   slice(3, 6, None),
   slice(4, 6, None),
   slice(5, 6, None),
   slice(6, 6, None)],
  6: [slice(1, 7, None),
   slice(2, 7, None),
   slice(3, 7, None),
   slice(4, 7, None),
   slice(5, 7, None),
   slice(6, 7, None),
   slice(7, 7, None)]}}
Community
  • 1
  • 1
jim jarnac
  • 4,804
  • 11
  • 51
  • 88

1 Answers1

2

define your function this way

def fun(slices):
    return [df.low.loc[s].tolist() for s in slices]

And apply over the slices column

df['slices_low'] = df.slices.apply(fun)

df

enter image description here

piRSquared
  • 285,575
  • 57
  • 475
  • 624
  • 1
    ^^ Man I've been looking for that for almost the entire day... We should do a brain swap for a day or 2!! Anyway thanks a lot! It's really great to have the opportunity to have people to help here. – jim jarnac Jan 04 '17 at 01:24
  • @jimbasquiat another really positive result is that you learn how to better articulate your questions and enable yourself to leverage SO even more. – piRSquared Jan 04 '17 at 01:29
  • 1
    Quick question why are you using .loc and not .iloc in your function? the slice are based on the integer value of the index no? – jim jarnac Jan 04 '17 at 01:29
  • Really good question!!! `iloc` is position based and as a result *excludes* the end point. You posted as your desired result something that included the end point. That requires the use of `loc` and assumes the slices are labels and not positions. If you want to rely on the slices being position based then you'll have to increment all of the the end points by one to produce the result you were looking for. – piRSquared Jan 04 '17 at 01:32
  • Quick extra question: how would you do to turn the function you created into a 1 line apply() with a lambda? – jim jarnac Jan 04 '17 at 02:57
  • 1
    `df['slices_low'] = df.slices.apply(lambda x: [df.low.loc[s].tolist() for s in x])` – piRSquared Jan 04 '17 at 02:58