All!
I have two [2] pd.DataFrames: df_colors [20,3] and df_master [200,13].
I am attempting to update the values for each item in each row.
df_colors.head(5)
| COLOR | CODE |
|---|---|
| WHITE | 0 |
| BLACK | 1 |
| GREEN | 0 |
| YELLOW | 0 |
| ... | ... |
| BLUE | 0 |
| RED | 1 |
df_master.head(5)
| color_0 | color_1 | color_2 | color_3 | color_4 | ... | color_12 | color_13 |
|---|---|---|---|---|---|---|---|
| RED | GREEN | YELLOW | PINK | NONE | ... | NONE | NONE |
| BLUE | BLACK | WHITE | GREEN | PINK | ... | RED | ORANGE |
| RED | NONE | NONE | NONE | NONE | ... | NONE | NONE |
| ... | |||||||
| BLACK | WHITE | ORANGE | NONE | NONE | ... | NONE | NONE |
| PURPLE | ORANGE | GREEN | WHITE | BLUE | ... | RED | NONE |
def fun2(x):
_y1 = df_colors[df_colors.COLOR==x]
if _y1.size > 0:
_y1 = _y1.values[0][1]
else:
continue
return _y1
col_names = list(df_master.columns)
dat1 = df_master[col_names] = df_master[col_names].map(lambda x: fun(2) if isinstance(x, str), else x)
dat1.head(3)
| color_0 | color_1 | color_2 | color_3 | color_4 | ... | color_12 | color_13 |
|---|---|---|---|---|---|---|---|
| 1 | Empty DataFrame Columns: [COLOR,CODE] | Empty DataFrame Columns: [COLOR,CODE] | Empty DataFrame Columns: [COLOR,CODE] | NONE | ... | NONE | NONE |
| 0 | Empty DataFrame Columns: [COLOR,CODE] | Empty DataFrame Columns: [COLOR,CODE] | Empty DataFrame Columns: [COLOR,CODE] | Empty DataFrame Columns: [COLOR,CODE] | ... | Empty DataFrame Columns: [COLOR,CODE] | Empty DataFrame Columns: [COLOR,CODE] |
| 1 | NONE | NONE | NONE | NONE | ... | NONE | NONE |
What I was expecting:
| color_0 | color_1 | color_2 | color_3 | color_4 | ... | color_12 | color_13 |
|---|---|---|---|---|---|---|---|
| 1 | 0 | 1 | 1 | NONE | ... | NONE | NONE |
| 0 | 1 | 0 | 0 | 1 | ... | 1 | 1 |
| 1 | NONE | NONE | NONE | NONE | ... | NONE | NONE |
| ... | |||||||
| 1 | 0 | 1 | NONE | NONE | ... | NONE | NONE |
| 1 | 1 | 0 | WHITE | 0 | ... | 1 | NONE |
Your current logic is unclear, but if you want to replace the colors by their code why not just use
map(applymapfor older pandas version):Output: