0

I don't know how to describe my problem in words, I just model it

Problem modeling:

Let say we have two dataframes df1, df2 with the same columns

df1

idx | col1 | col2 | col3 |  col4 
---------------------------------
 0  |  1   | -100 |   2  |  -100 

df2

idx | col1 | col2 | col3 |  col4
---------------------------------
 0  |  12  |  23  |  34  |   45 

Given these two df-s we get

df_result

idx | col1 | col2 | col3 |  col4 
---------------------------------
 0  |  1   |  23  |   2  |   45 

I.e. we get df1 where all -100 substituted with values from df2 accordingly.

Question: How can I do it without for-loop? In particular, is there an operation in pandas or on two lists of the same size that could do what we need?

PS: I can do it with for loop but it will be much slower.

1

1 Answer 1

2

You can use this:

df1[df1==-100] = df2

This is how it works step-by-step:

import pandas as pd
import numpy as np

df1 = pd.DataFrame(np.array([[1,-100,2,-100],[-100,3,-100,-100]]), columns=['col1','col2','col3','col4'])
df1

col1    col2    col3    col4
1       -100    2       -100
-100    3       -100    -100


df2 = pd.DataFrame(np.array([[12,23,34,45],[1,2,3,4]]), columns=['col1','col2','col3','col4'])
df2

col1    col2    col3    col4
12      23      34      45
1       2       3       4

By using boolean indexing you have that

df1==-100

col1    col2    col3    col4
False   True    False   True
True    False   True    True

So when True you can assign the corresponding value of df2:

df1[df1==-100]=df2
df1

col1    col2    col3    col4
1       23      2       45
1       3       3       4
Sign up to request clarification or add additional context in comments.

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.