1

I'm having a question on python. I'm trying to compare two dataframes and check which elements are different and insert them into another dataframe. So here are my dataframes. df1:

PN      Stock        WHS      Cost
1111     1           VLN       0.2
1111     2           VLN       0.2
1115     1           KNS       0.5 

df2:

    PN      Stock        WHS      Cost    Time
   1111        1         VLN       0.2     15:00
   1111        3         VLN       0.2     16:00

So the idea is to add to df1 the data from df2 that is not yet in df1; line 2 in df2 does not exist in df1 so I want to insert it. How should i write code to find not existing line so that I could insert it? I have tried:

   for index, row in df1.iterrows():
        if df2[(df2['PN']==row['PN']) & (df2['Stock'] ==row['Stock']) & (df2['Whs'] = row['Whs']) & (df2['Cost']==row['Cost'])].empty
              print row['PN']

To check which rows to update but i get basically all rows printed, not the ones that do not match. How can I solve this please? Is it possible to use somehow 'IN' function, comparing each df1 line with whole df2???

3
  • line 2 in List2 does not exist in List1?? It does. Only the Time columns does not exist... Commented May 19, 2015 at 8:05
  • Your question doesn't make sense can you post desired output, also you state line2 in your 2nd df does not exist but it does Commented May 19, 2015 at 8:08
  • sorry guys, updated question, now line 2 in list2 does not exist in list1 Commented May 19, 2015 at 8:14

2 Answers 2

1

I believe you want to perform an outer merge:

In [29]:

df.merge(df1, how='outer')
Out[29]:
     PN  Stock  WHS  Cost   Time
0  1111      1  VLN   0.2  15:00
1  1111      2  VLN   0.2    NaN
2  1115      1  KNS   0.5    NaN
3  1111      3  VLN   0.2  16:00
Sign up to request clarification or add additional context in comments.

Comments

1

You can use outer join to achieve this result

    pd.concat([df1,df2],join='outer')

http://pandas.pydata.org/pandas-docs/stable/generated/pandas.concat.html for more options about how you want to deal with indexes etc..

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.