title: how to recode the categorical variable to ordinal based on the event rate of another variable in python?
body: <p>I have a dataframe in python with many categorical variables and the target variable is binary. I want to convert the categorical variable to ordinal based on the rank of target variable event rate (same to mean of target variable) on each category of the variable. For example, if below is my original dataset</p>
  
  <p><a href="https://i.stack.imgur.com/qcau8.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/qcau8.png" alt="enter image description here"></a></p>
  
  <p>For each category on column1, aka, 'A' and 'B', we have:
  <a href="https://i.stack.imgur.com/kzFMR.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/kzFMR.png" alt="enter image description here"></a></p>
  
  <p>For each categry on column2, aka, 'C','D','E','F','G', we have:</p>
  
  <p><a href="https://i.stack.imgur.com/J1qCg.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/J1qCg.png" alt="enter image description here"></a></p>
  
  <p>So I want to be able to create the final dataset like this:</p>
  
  <p><a href="https://i.stack.imgur.com/Oumxn.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Oumxn.png" alt="enter image description here"></a></p>
  
  <p>How can i create one like that?</p>
  
  <p>Thanks!!!!</p>