2

I have a dataframe that has values of the different column numbers for another dataframe. Is there a way that I can just return the value from the other dataframe instead of just having the column index.

I basically want to match up the index between the Push and df dataframes. The values in the Push dataframe contain what column I want to return from the df dataframe.

Push dataframe:

    0   1
0   1   2
1   0   3
2   0   3
3   1   3
4   0   2

df dataframe:

    0   1   2   3   4
0   10  11  22  33  44
1   10  11  22  33  44
2   10  11  22  33  44
3   10  11  22  33  44
4   10  11  22  33  44

return:

    0   1
0   11  22
1   10  33
2   10  33
3   11  33
4   10  22
1
  • Could you rephrase your question, and what exactly are you expecting in the output, also how are the two dataframes related to each other? Commented Dec 4, 2017 at 20:23

2 Answers 2

2

You can do it with np.take ; However this function works on the flattened array. push must be shift like that :

 In [285]: push1 = push.values+np.arange(0,25,5)[:,None]
 In [229]: pd.DataFrame(df.values.take(push1))

EDIT

No, I just reinvent np.choose :

In [24]: df
Out[24]: 
    0   1   2   3   4
0   0   1   2   3   4
1  10  11  12  13  14
2  20  21  22  23  24
3  30  31  32  33  34
4  40  41  42  43  44

In [25]: push
Out[25]: 
   0  1
0  1  2
1  0  3
2  0  3
3  1  3
4  0  2

In [27]: np.choose(push.T,df).T
Out[27]: 
    0   1
0   1   2
1  10  13
2  20  23
3  31  33
4  40  42
Sign up to request clarification or add additional context in comments.

1 Comment

it works only because lines are equal :( ; must be tuned.
0

We using melt then replace notice (df1 is your push , df2 is your df)

df1.astype(str).replace(df2.melt().drop_duplicates().set_index('variable').value.to_dict())
Out[31]: 
    0   1
0  11  22
1  10  33
2  10  33
3  11  33
4  10  22

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.