2

I have a question regarding merging two rows of different dataframes in Python pandas.

Dataset A (imported as csv):

Index Parameter 1 Parameter 2 Parameter 3 Parameter 4
1 A10 1 4 8
2 B06 5 5 5
3 D04 6 3 4

Dataset 2 (imported as csv):

Index Parameter A Parameter B Parameter C Parameter D
1 B06 0 2 5.5
2 B06 2 4 4.5
3 B06 4 6 7.4
4 B06 6 8 8.8

I want the program to go through every row of Dataset A (with a for loop) and find the suitable row in Dataset B and merge them.

The suitable row in Dataset B is definde as

Parameter 1 = Parameter A

Parameter B < Parameter 2 <= Parameter C

The result should be look as:

Index Parameter 1/A Parameter 2 Parameter 3 Parameter 4 Parameter B Parameter C Parameter D
1 A10 1 4 8 - - -
2 B06 5 5 5 4 6 7.4
3 D04 6 3 4 - - -

Each row auf Dataset A has a suitable row in Dataset B.

I'm quite new in Python and ask politely for your help. Let me know, if you have some ideas, which can bring me further! Thank you so much!

1 Answer 1

1

Merge and filter:

(df1.merge(df2.rename(columns={'Parameter A':'Parameter 1'}),
           on='Parameter 1')
    .loc[lambda x: x['Parameter B'].lt(x['Parameter 2']) & x['Paremeter 2'].le(x['Parameter C'] ]
)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.