How to encode two Pandas dataframes according to the same dummy vectors?
<p>I'm trying to encode categorical values to dummy vectors.
pandas.get_dummies does a perfect job, but the dummy vectors depend on the values present in the Dataframe. How to encode a second Dataframe according to the same dummy vectors as the first Dataframe?</p>

<pre><code> import pandas as pd


df=pd.DataFrame({'cat1':['A','N','K','P'],'cat2':['C','S','T','B']})
b=pd.get_dummies(df['cat1'],prefix='cat1').astype('int')
print(b)



  cat1_A  cat1_K  cat1_N  cat1_P
0       1       0       0       0
1       0       0       1       0
2       0       1       0       0
3       0       0       0       1



df_test=df=pd.DataFrame({'cat1':['A','N',],'cat2':['T','B']})
c=pd.get_dummies(df['cat1'],prefix='cat1').astype('int')
print(c)

   cat1_A  cat1_N
0       1       0
1       0       1
</code></pre>

<p>How can I get this output ? </p>

<pre><code> cat1_A  cat1_K  cat1_N  cat1_P
0       1       0       0       0
1       0       0       1       0
</code></pre>

<p>I was thinking to manually compute uniques for each column and then create a dictionary to map the second Dataframe, but I'm sure there is already a function for that...
Thanks!</p>

<p>A always use <a href="https://github.com/wdm0006/categorical_encoding" rel="nofollow">categorical_encoding</a> because it has a great choice of encoders. It also works with Pandas very nicely, is pip installable and is written inline with the sklearn API.</p>

<p>If you wish to encode just the first column, like in your example, we can do so.</p>

<pre><code>import pandas as pd
import category_encoders as ce

df = pd.DataFrame({'cat1':['A','N','K','P'], 'cat2':['C','S','T','B']})
enc_ohe = ce.one_hot.OneHotEncoder(cols=['cat1']) # cols=None, all string columns encoded

df_trans = enc_ohe.fit_transform(df)
print(df_trans)

   cat1_0  cat1_1  cat1_2  cat1_3 cat2
0       0       1       0       0    C
1       0       0       0       1    S
2       1       0       0       0    T
3       0       0       1       0    B
</code></pre>

<p>The only downside with it here is that it the column names have numerical encoding instead of the original letters. This is helpful though when you have long strings as categories.</p>

<p>Now we can use the <code>transform</code> method to encode your second DataFrame.</p>

<pre><code>df_test = pd.DataFrame({'cat1':['A','N',],'cat2':['T','B']})
df_test_trans = enc_ohe.transform(df_test)

print(df_test_trans)

   cat1_1  cat1_3 cat2
0       1       0    T
1       0       1    B
</code></pre>

<p>As commented in line 5, not setting <code>cols</code> defaults to encode all string columns.</p>

python pandas machine-learning