Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

search value by row column

I have a dataframe(df1). each cell means row, column indexes:

c1 c2 c3
0 1,1 2,1 3,2
1 1,1 2,2 3,1

How to perform search by row, column within the following dataframe(df2)

c1 c2 c3 c4
0 1 2 10 x
1 3 4 20 y
2 5 6 30 x

for example 1,1 of df1 is 1, 2,1 is 3, 3,2 is 6.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Final output for 3,2 should be: 180, x: (6(which is df2 3rd row, 2nd column)*30(3rd row, 3rd column),x = 180, x)

>Solution :

First set index and columns values to range starting by 1, so possible select by splitted values converted to integers by DataFrame.loc elementwise by DataFrame.applymap:

df22 = df2.rename(index = lambda x: x + 1)
          .set_axis(np.arange(1, len(df2.columns) + 1), inplace=False, axis=1)
f = lambda x: df22.loc[tuple(map(int, x.split(',')))]
df = df1.applymap(f)
print (df)
   c1  c2  c3
0   1   3   6
1   1   4   5
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading