Reputation: 67
I have three data frames d1,d2,d3 with same column names, I want to create a new data frame when if d1 is 1 then d2 value will be assigned if not value of d3 will be assigned.
Upvotes: 1
Views: 50
Reputation: 153460
Try using mask
:
df3.mask(df1.astype(bool), df2)
Or, where
:
df2.where(df1.astpye(bool), df3)
MCVE:
df1 = pd.DataFrame({'X1':[1]*4, 'X2':[0]+[1]*3, 'X3':[0]+[1]*3}, index = pd.date_range('7/10/2019', periods=4, freq='D'))
X1 X2 X3
2019-07-10 1 0 0
2019-07-11 1 1 1
2019-07-12 1 1 1
2019-07-13 1 1 1
df2 = pd.DataFrame(np.random.randint(0,100, (4,3)), columns=['X1','X2','X3'], index = pd.date_range('7/10/2019', periods=4, freq='D'))
X1 X2 X3
2019-07-10 66 92 98
2019-07-11 17 83 57
2019-07-12 86 97 96
2019-07-13 47 73 32
df3 = pd.DataFrame(np.random.randint(10000,1000000, (4,3)), columns=['X1','X2','X3'], index = pd.date_range('7/10/2019', periods=4, freq='D'))
X1 X2 X3
2019-07-10 699070 524272 206719
2019-07-11 767145 420963 569950
2019-07-12 472077 762036 418160
2019-07-13 664544 879444 869953
Output:
X1 X2 X3
2019-07-10 66 524272 206719
2019-07-11 17 83 57
2019-07-12 86 97 96
2019-07-13 47 73 32
Upvotes: 1