2

I would like to implement a specific join operation with the following requirements:

I have a data frame in the following format, where the index is datetime and I have columns from 0 to N (9 in this example)

df1:

 0 1 2 3 4 5 6 7 8 9 2001-01-01 2 53 35 91 43 31 7 87 25 68 2001-01-02 12 97 86 59 51 7 75 25 6 40 2001-01-03 73 82 87 1 46 66 17 42 96 61 

I also have another dataframe that contains the columns to be chosen for each datetime index, i.e. the values are 0 to N:

 0 2001-01-01 9 2001-01-02 5 2001-01-03 4 

I would like to select the underlying values of the first dataframe, where

index df1 = index df2 columns df1 = value df2 

For example the results for the above example should look like this:

join(df1,df2)= 0 2001-01-01 68 2001-01-02 7 2001-01-03 46 
1
  • So, could there be some date indices missing in df2 that could be part of df1 or vice versa? If so, what must be the desired behavior then? Could you post a sample case for such a situation? Commented Jul 14, 2016 at 18:19

2 Answers 2

3

You can use lookup:

print (df1.lookup(df1.index, df2.iloc[:,0])) [68 7 46] print (pd.DataFrame(df1.lookup(df1.index, df2.iloc[:,0]), index=df1.index)) 0 2001-01-01 68 2001-01-02 7 2001-01-03 46 

Another solution with squeeze:

print (pd.DataFrame(df1.lookup(df1.index, df2.squeeze()), index=df1.index)) 0 2001-01-01 68 2001-01-02 7 2001-01-03 46 
Sign up to request clarification or add additional context in comments.

1 Comment

I would think this would be faster, as it avoids any conversion to array. And good to see a NumPy alternative for pandas to do such fancy indexing.
3

Something along these lines taken from NumPy's indexing methods -

vals = df1.values[np.arange(df1.shape[0]),df2[0].values] df_out = pd.DataFrame(vals,index=df1.index) 

1 Comment

@motam79 Also look into @ jezrael's solution as that might be faster.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.