1

This is a subset of data frame F1:

id        code    s-code
l.1        1       11
l.2        2       12
l.3        3       13
f.1        4       NA
f.2        3        1
h.1        2        1
h.3        1        1

I need to compare the F1.id with F2.id and then add the differences in column "id" to the F2 data frame and fill in columns' values for the added "id" with 0.

this is the second data frame F2:

id        head    sweat  pain
l.1        1       0      1
l.3        1       0      0
f.2        3        1     1
h.3        1        1     0

The output should be like this:

F3:

id        head    sweat  pain
l.1        1       0      1
l.3        3       13     0  
f.2        3        1     1
h.1        2        1     1
h.3        1        1     0
l.2        0        0     0
h.1        0        0     0
f.1        0        0     0

I tried different solution, such as F1[(F1.index.isin(F2.index)) & (F1.isin(F2))] to return the differences, but non of them worked.

2
  • Check your expected output... there's a 13 in there. Is that a mistake? Commented Oct 4, 2017 at 22:43
  • 1
    How you got two h.1 ..in column Id Commented Oct 4, 2017 at 22:47

3 Answers 3

4

By using reindex

df2.set_index('id').reindex(df1.id).fillna(0).reset_index()
Out[371]: 
    id  head  sweat  pain
0  l.1   1.0    0.0   1.0
1  l.2   0.0    0.0   0.0
2  l.3   1.0    0.0   0.0
3  f.1   0.0    0.0   0.0
4  f.2   3.0    1.0   1.0
5  h.1   0.0    0.0   0.0
6  h.3   1.0    1.0   0.0
Sign up to request clarification or add additional context in comments.

7 Comments

Very nice. You can shorten this: df2.set_index('id').reindex(df['id'], fill_value=0).reset_index() - bonus: you get int columns, so no need to convert.
It also preserves the dtypes
@cᴏʟᴅsᴘᴇᴇᴅ Yes you are right :) much nicer! BTW ...I do not think I get the expected out put he want ....
It's the same as mine though? OP seemed to approve of it.
@cᴏʟᴅsᴘᴇᴇᴅ Base on his description , ours solution should be ok, but if you look at his expected out put ....
|
3

Use an outer merge + fillna:

df[['id']].merge(df2, how='outer')\
            .fillna(0).astype(df2.dtypes)

    id  head  sweat  pain
0  l.1     1      0     1
1  l.2     0      0     0
2  l.3     1      0     0
3  f.1     0      0     0
4  f.2     3      1     1
5  h.1     0      0     0
6  h.3     1      1     0

3 Comments

@COLDSPEED, Thank you. Is there anyway not to mention the name of columns from df2? I have about 40 columns in df2.
astype(df2.dtypes)
piRSquared, Wonderful. I didn't know you could do that. @Mary, you have your answer.
3

Outside the Box

i = np.setdiff1d(F1.id, F2.id)
F2.append(pd.DataFrame(0, range(len(i)), F2.columns).assign(id=i))

    id  head  sweat  pain
0  l.1     1      0     1
1  l.3     1      0     0
2  f.2     3      1     1
3  h.3     1      1     0
0  f.1     0      0     0
1  h.1     0      0     0
2  l.2     0      0     0

With a normal index

i = np.setdiff1d(F1.id, F2.id)
F2.append(
    pd.DataFrame(0, range(len(i)), F2.columns).assign(id=i),
    ignore_index=True
)

    id  head  sweat  pain
0  l.1     1      0     1
1  l.3     1      0     0
2  f.2     3      1     1
3  h.3     1      1     0
4  f.1     0      0     0
5  h.1     0      0     0
6  l.2     0      0     0

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.