0

I have a data frame (df1) like this:

           X        Y
1          200.0    50            
2          200.1    57    
3          200.2    69
4          200.3    77
5          200.5    84
6          200.6    93

and I have another data frame (df2) like this:

           X
1          200.0                
2          200.5    

I want to extract the Y-values of df1 which match to the X-Values of df2 into the df2 that it looks like this:

           X        Y
1          200.0    50                 
2          200.5    84

How can I solve this problem for example with pandas and numpy ? Unfortunately I'm quite new in python and I have no idea.

Thank you.

Best Regards, DocSnyda

docsynda
  • 3
  • 1

1 Answers1

0

pd.merge() is the first thing we would think of when the requirement of "looking things up in another df" comes up, but df.loc[] itself does have "looking things up" meaning as well.

"""set the df1 and df2 up """
import pandas as pd
import numpy as np

s ="""20000
20000
20000
200.4
200.5
200.6"""  
df1 = pd.DataFrame(s.split('\n'), columns=['X'])

df1['Y'] = """50
57
69
77
84
93""".split('\n')

df2 = df1.iloc[[0, 5], :]
df2 = df2.drop(columns=['Y'])
print(df1)
print(df2)



""" the anwser to your question here: """
print(
    df1.loc[df1.X.isin(df2.X), :].drop_duplicates(subset='X')
)
eliu
  • 2,390
  • 1
  • 17
  • 29
  • Thanks a lot this works great!! Is there a way to avoid to extract double X-values ? There some rows with the same X-value and I want to extract just one of them. It doesn't matter which X-value will be extracted because they are the same. – docsynda Apr 25 '19 at 20:50
  • edited to meet new requirement. please remember to accept and upvote. thanks – eliu Apr 25 '19 at 23:31