Match three 'isin' statements and return index where they match, then replace value if all conditions are satisfied

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


Match three 'isin' statements and return index where they match, then replace value if all conditions are satisfied



I want to find where three different columns in two dataframes match and then replace a value in another column if they match. How do I combine the three conditions below, perhaps in a loop form, to check if each row matches all three conditions?



First two conditions - matching the values in two different columns:


DATA1[DATA1[‘f_code’].isin(DATA2[‘F’])] # Column 1
DATA1[DATA1[‘b_id’].isin(DATA2[‘B’])] # Column 2



Last condition which I am writing wrong. I want the value in DATA1 'species' to match the header of DATA2 if the above two conditions are true
DATA1[DATA1[‘species’] == DATA2[header = ‘SO2’]



Then, if the conditions are met in that row, I want to replace the value the column named 'value' in DATA1 with the value that was under the 'SO2' header for that row.



How can I do this?



I tried this (not written correctly):


for a, elem in enumerate(DATA1['f_code']):
if DATA1['f_code'].loc[a] in DATA2['F'] and DATA1['b_id'].loc[a] in DATA2['B']
replace(DATA1['val'] with DATA2['value']



Example:



DATA1


Species Value f_code B
50000 0.00 55933 TPP1
**NOX 0.38 55933 TPP1
NOX 0.48 55933 TPP1**
SO2 0.02 55933 TPP1
50000 0.00 55933 TPP2
NOX 0.31 55933 TPP2
SO2 0.01 55933 TPP2
50000 0.07 6002 1
NOX 2377.90 6002 1
SO2 2354.10 6002 1
50000 0.07 6002 2
NOX 2998.60 6002 2
SO2 531.00 6002 2



DATA2


F B Nox SO2
6002 1 1151.97 152.98
6002 2 1550.17 213.3
6002 3 2428.71 226.26
6002 4 2135.05 219.42
55510 GT-1 0.85 0.05
55542 1 3.05 0.10
55542 2 3.17 0.11
55933 TPP1 11.33 0.64
55933 TPP2 12.67 0.71



RESULT


Species Value f_code B
50000 0.00 55933 TPP1
**NOX 5.01 55933 TPP1
NOX 6.33 55933 TPP1**
SO2 0.64 55933 TPP1
50000 0.00 55933 TPP2
NOX 12.67 55933 TPP2
SO2 0.71 55933 TPP2
50000 0.07 6002 1
NOX 1151.97 6002 1
SO2 152.98 6002 1
50000 0.07 6002 2
NOX 1550.17 6002 2
SO2 213.31 6002 2



Note: For the bolded part, note that there were two values with the same f_code and b_id in DATA1, but only one value in DATA2. In this situation, I want to replace the values in DATA1 with the weighted value in DATA2


11.33*(0.38/(0.38+0.48)) = 5.01
11.33*(0.48/(0.38+0.48)) = 6.33





Can you post a Minimal, Complete, and Verifiable example ?
– RafaelC
42 mins ago





is it not the same question than yours 1h ago?
– Ben.T
31 mins ago







Added example dataset
– shizishan
26 mins ago









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Arduino Mega cannot recieve any sketches, stk500_recv() programmer is not responding

Visual Studio Code: How to configure includePath for better IntelliSense results

C++ virtual function: Base class function is called instead of derived