OctoCatKnows
OctoCatKnows

Reputation: 1

Why is .apply / .map only running against the first column?

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

Upvotes: 0

Views: 43

Answers (1)

mozway
mozway

Reputation: 262284

Your current logic is unclear, but if you want to replace the colors by their code why not just use map (applymap for older pandas version):

out = df_master.map(df_colors.set_index('COLOR')['CODE'].get)

Output:

   color_0  color_1  color_2  color_3  color_4  color_12 color_13
0      1.0      0.0      0.0      NaN      NaN       NaN     None
1      0.0      1.0      0.0      0.0      NaN       1.0     None
2      1.0      NaN      NaN      NaN      NaN       NaN     None
3      1.0      0.0      NaN      NaN      NaN       NaN     None
4      NaN      NaN      0.0      0.0      0.0       1.0     None

Upvotes: 0

Related Questions