0

I'm trying to find the most optimized/Pythonesque way of doing the following (and didn't see similar problem already addressed on SO): For each columns in df1, indexed by date, I'd like to keep a (time) slice, defined in df2 (where the rows in column "key" correspond to the indexes in df1) and fill the rest with NaN's. ie :

df1:

         A   B   C
01/01   10  100  1
02/01   20  200  2
03/01   30  300  3
04/01   40  400  4
05/01   50  500  5
06/01   60  600  5

and

df2:

    keys   start    end   
0    A     02/01    04/01
1    B     04/01    06/01
2    C     01/01    04/01

would result in :

         A   B     C
01/01   NaN  NaN   1
02/01   20   NaN   2
03/01   30   NaN   3
04/01   40   400   4
05/01   NaN  500  NaN
06/01   NaN  600  NaN

Until now, the only solutions I'm finding are by iterating over each columns of df1, and using loc with reindexed version of df2, both things I don't really like doing in Pandas (but perhaps am I wrong ?). Any other idea how to do what seems to be a basic Pandas' problem (but got me quite stuck) ? Thank you!

DrGorilla.eth
  • 220
  • 2
  • 11
  • Indeed, was only searching for "slices" questions instead of "merge" ones, thank you! – DrGorilla.eth Feb 19 '20 at 14:04
  • Hmm, ok, no, not exactly the same problematic (I have multiple columns to slice and I'd like to avoid looping over them, which is not adressed in the ref answer)... Guess I'll have to keep on looking by myself. – DrGorilla.eth Feb 19 '20 at 14:11

0 Answers0