2

I am a pandas rookie and I have reviewed similar questions in stackoverflow, but this seems unique.

I am looking for a function that will compare A and B and if any column in B has a value > 0 for the column, the DataFrame B will be used to create DataFrame C.

The goal is for DataFrame C the same size as DataFrame A, just with DataFrame B's values for the columns with the same label.

Have: 
A = pd.DataFrame({"X1": [0], "Y1": [0], "X2": [0], "Y2": [0], "X3": [0], "Y3": [0], "X4": [0], "Y4": [0]})

B = pd.DataFrame({"X1": [9], "Y1": [99.9]})

Want:

C= pd.DataFrame({"X1": [9], "Y1": [99.9], "X2": [0], "Y2": [0], "X3": [0], "Y3": [0], "X4": [0], "Y4": [0]})

3
  • What have you tried so far ? Commented Dec 2, 2020 at 21:33
  • 1
    So will DataFrame A always have only 0 as values, as your example seems to suggest ? Commented Dec 2, 2020 at 22:05
  • Can B contain keys not in A? Can you just for b in B: or would that have illegal values? Commented Dec 2, 2020 at 22:05

3 Answers 3

2

You can use pandas update where B > 0:

C = A.copy()
C.update(B.where(B>0))

Output:

   X1    Y1  X2  Y2  X3  Y3  X4  Y4
0   9  99.9   0   0   0   0   0   0
Sign up to request clarification or add additional context in comments.

1 Comment

That's a great solution!
1

I have a solution but is seems a bit forced

import pandas as pd


A = pd.DataFrame({"X1": [0], "Y1": [0], "X2": [0], "Y2": [
                 0], "X3": [0], "Y3": [0], "X4": [0], "Y4": [0]})

B = pd.DataFrame({"X1": [9], "Y1": [99.9]})

C = pd.concat([A, B])

D = C.fillna(0)

E = D.iloc[1:]

print (E)

Ideas are welcome!

Comments

1

It's a little ugly but it gets the job done

A = pd.DataFrame({"X1": [0], "Y1": [0], "X2": [0], "Y2": [0], "X3": [0], "Y3": [0], "X4": [0], "Y4": [0]})
B = pd.DataFrame({"X1": [9], "Y1": [99.9]})

B = B.loc[:, (B > 0).any(axis=0)]
cols = B.columns.tolist()
C = A
C[cols] = B[cols]

2 Comments

Here, you're also changing the values of A by reference. I'd create C as a copy of A.
Good point agreed, I prefer your solution anyways :)

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.