1

my first dataframe

dataframe1=

id   number  count1 count2  
1   5225    10       30 
2   2222    3        40
3   7121    30        50

second dataframe

dataframe2=

id  value1  value2 
1   4000     6000
3   2500     3300
3   7000     8000

output

id  value1  value2 number count1 count2
1   4000     6000  5225  10       30
3   7000     8000  7121  30       50

I tried

dataframe2['a']=(dataframe1['number']>= dataframe2['value1']) & (dataframe1['number'] <= dataframe2['value2'])

it gave True False values in column a, What I want to do is this: For each value in the Dataframe1 "number" column, search if it is equal to or between ANY of the "value1" and "value2" pair values of Dataframe2. Additionally, for this "number1" and "number2" pair values, its respective "id" must match the "id" in Dataframe. If this is all true, then I want to insert along with number, count1 and count2 in dataframe2

2 Answers 2

1
merged = df1.join(df2, on="id", how="inner")
result = merged.query("value1 <= number <= value2")
result

    number  count1  count2  value1  value2
id                                        
1     5225      10      30    4000    6000
3     7121      30      50    7000    8000
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your ansr mboss, without join on id can we do? let's assume there is no ID column
1

First, let's create the dataframes from your example:

DataFrame 1

import pandas as pd  

d1 = {'id': [1, 2, 3],
     'number': [5225, 2222, 7121],
     'count1': [10, 3, 30],
     'count2': [30, 40, 50]}
df1 = pd.DataFrame(data = d1)
print(df1)

id  number  count1  count2
1   5225    10      30
2   2222    3       40
3   7121    30      50

DataFrame 2

d2 = {'id': [1, 3, 3],
     'value1': [4000, 2500, 7000],
     'value2': [6000, 3300, 8000]}
df2 = pd.DataFrame(data = d2)
print(df2)

id  value1  value2
1   4000    6000
3   2500    3300
3   7000    8000

One possible solution is to first join the two tables together:

df3 = df1.join(other = df2.set_index('id'),
              on = 'id')
print(df3)

id  number  count1  count2  value1  value2
1   5225    10      30      4000.0  6000.0
2   2222    3       40      NaN     NaN
3   7121    30      50      2500.0  3300.0
3   7121    30      50      7000.0  8000.0

Then filter by multiple conditions:

df3[(df3['number'] >= df3['value1']) & (df3['number'] <= df3['value2'])]


id  number  count1  count2  value1  value2
1   5225    10      30      4000.0  6000.0
3   7121    30      50      7000.0  8000.0

5 Comments

Thank you Josiah, worked well .. thank you for your answer
hi Josaih, without join on ID can we do this one
There is a one more value 2222 which is comming under value1(2500) and value2(3300) i want that type values also
You can join without join on ID, however, the output is different than your desired outcome. You'll have ID value1 & value2 for numbers 5225 and 7121 will result in NaNs.
To add value 2222 with value1 as 2500 and value2 as 3300: d2 = {'id': [1, 3, 3, 2], 'value1': [4000, 2500, 7000, 2500], 'value2': [6000, 3300, 8000, 3300]}

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.