I have the following dataframes:
df1:
+-----+------+------+------+------+------+
| No. | col1 | col2 | col3 | Type | ... |
+-----+------+------+------+------+------+
| 123 | 2 | 5 | 2 | MN | ... |
| 453 | 4 | 3 | 1 | MN | ... |
| 146 | 7 | 9 | 4 | AA | ... |
| 175 | 2 | 4 | 3 | MN | ... |
| 643 | 0 | 0 | 0 | NAN | ... |
+-----+------+------+------+------+------+
df2:
+-----+------+------+------+------+
| No. | col1 | col2 | col3 | Type |
+-----+------+------+------+------+
| 123 | 24 | 57 | 22 | MN |
| 453 | 41 | 39 | 15 | MN |
| 175 | 21 | 43 | 37 | MN |
+-----+------+------+------+------+
I want to replace col1, col2 and col3 in df1 with corresponding values in df2 if Type equals MN
Desired output:
df1:
+-----+------+------+------+------+-----+
| No. | col1 | col2 | col3 | Type | ... |
+-----+------+------+------+------+-----+
| 123 | 24 | 57 | 22 | MN | ... |
| 453 | 41 | 39 | 15 | MN | ... |
| 146 | 7 | 9 | 4 | AA | ... |
| 175 | 21 | 43 | 37 | MN | ... |
| 643 | 0 | 0 | 0 | NAN | ... |
+-----+------+------+------+------+-----+
EDIT
I tried:
df1[df1.Type == 'MN'] = df2.values
but I get this error:
ValueError: Must have equal len keys and value when setting with an ndarray
Guess the reason is, that df2 does not have equal number of columns. So how do I make sure, that only the specific columns (col1 - col3) are replaced in df1?