0

I have a df like this:

      xx   yy   zz
 A    6     5    2
 B    4     4    5
 B    5     6    7
 C    6     6    6
 C    7     7    7

Then I have a dictionary with some keys (which correspond to the index names of the df) and values (column names):

{'A':['xx'],'B':['yy','zz'],'C':['xx','zz']}

I would like to use the dictionary to check that those column names that do not appear in the dict values , are set to zero to generate this output:

      xx   yy   zz
 A    6     0    0
 B    0     4    5
 B    0     6    7
 C    6     0    6
 C    7     0    7

How could I use the dictionary to generate the desired output?

rafaelc
  • 57,686
  • 15
  • 58
  • 82
JamesHudson81
  • 2,215
  • 4
  • 23
  • 42

2 Answers2

3

You may use indexing

mask = (pd.DataFrame(d.values(), index=d.keys())
          .stack()
          .reset_index(level=1, drop=True)
          .str.get_dummies()
          .groupby(level=0).sum()
          .astype(bool)
        )

df[mask].fillna(0)

    xx   yy   zz
A  6.0  0.0  0.0
B  0.0  4.0  5.0
B  0.0  6.0  7.0
C  6.0  0.0  6.0
C  7.0  0.0  7.0
rafaelc
  • 57,686
  • 15
  • 58
  • 82
2

What I will do

s=pd.Series(d).explode()
s=pd.crosstab(s.index,s)

df.update(s.mask(s==1))
df
    xx   yy   zz
A  6.0  0.0  0.0
B  0.0  4.0  5.0
B  0.0  6.0  7.0
C  6.0  0.0  6.0
C  7.0  0.0  7.0
BENY
  • 317,841
  • 20
  • 164
  • 234