I want to append string based on values in other table.
DF1:
Tech_ID s1 s2 s3 s4 s5 s6
-------------------------------
1 1 1 1 0 1 1
2 1 0 1 1 1 0
3 1 1 0 0 1 1
Df1 = {'Tech_ID':[1,2,3],
'S1':[1,1,1], 'S2':[1,0,1], 'S3': [1,1,0],
'S4':[0,1,0],'S5':[1,1,1],'S6':[1,0,1]}
Same way Df2 is like
Df2:
Tech_ID Std_Id s1_Norm s2 Norm S3 Norm S4_Norm S5 Norm S6_Norm
---------------------------------------------------------------------
1 1 11 01 10 11 00 01
1 7 01 11 01 11 01 10
2 4 00 10 00 11 10 11
3 8 10 11 01 01 11 00
2 2 11 01 00 10 01 10
.
.
.
Df2 = {'Tech_ID':[1,1,2,3,2], Std_Id [1,7,4,8,2]
'S1_norm':[11,01,00,10,11], 'S2_norm':[01,11,10,11,01],
'S3_norm': [10,01,00,01,00], 'S4_norm':[11,11,11,01,10],
'S5_norm': [00,01,10,11,01], 'S2_norm':[01,10,11,00,10],}
Now want to generate the string based on DF1 Values
For Tech_ID: 1
- s1+s1_Norm+s2+s2_Norm+s3+s3_Norm+s5+s5_Norm+s6+s6_Norm Because s4=0 so not consider value of s4 & s4_Norm in string.
Same way for Tech_ID: 2
- s1+s1_Norm+s3+s3_Norm+s4+s4_Norm+s5+s5_Norm same here s2 and s6 are 0 so s2,s2_norm,s6 and s6_norm not consider in string.
s4 and s4_norm are not taken in string calculation as s4 value is 0.
Same way any S series value 0 then value of that s bit and s_Norm bit not considered in string generation.
Expected result:
Tech_ID Std_Id Result
---------------------------------
1 1 111101110100101
1 7 101111101101110
2 4 100100111110
3 8 100111111100
2 2 111100110101
.
.
.
Same way if s4= 1 and s3=0 then s3 and s3_Norm should not consider in string generation.
try to do with below code in not consider Tech_id not in it, but not get succeed.
import itertools
#(df1.astype(str).values + df2) @ df1.T.values
-------
d_ = (df1.astype(str).values + df2.iloc[:,3:]).apply(lambda row: ''.join(itertools.compress(row.tolist(), df1.loc[0].tolist())), axis=1)
df_ = pd.concat([df2.iloc[:, :2], d_], axis=1)