0

I am relatively new to Pandas and Python. I am looking for advice on the quickest way to complete the following task. I am trying to write a code which would be similar to the pd.get_dummies() command, but generalizes it to more than one column.

I want to select the columns dx1, dx2, and dx3, and return a dummy for whether any of these columns includes a given value.

An example of input data:

In [2]: df
Out[2]: 
   dx1  dx2   dx3  othervar
0  1     2    3       4
1  1     3            5
2  3     4            2

Output data:

In [2]: df
Out[2]: 
   dx1  dx2   dx3  othervar  dx_any1    dx_any2    dx_any 3   dx_any4
0  1     2    3       4        1          1           1         0
1  1     3            5        1          0           1         0
2  3     4            2        0          0           1         1  
Maggie S
  • 1
  • 1
  • 2
    Welcome to StackOverflow. Please take the time to read this post on [how to provide a great pandas example](http://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) and edt your qustion with sample input v/s expected output – anky Jan 23 '20 at 16:56
  • After some digging, I have found an answer for my question in another post: https://stackoverflow.com/questions/52028442/pandas-get-dummies-on-multiple-columns – Maggie S Jan 23 '20 at 17:17
  • Flagged as duplicate of [Pandas get\_dummies on multiple columns](https://stackoverflow.com/questions/52028442/pandas-get-dummies-on-multiple-columns) – Eric Brandt Jan 23 '20 at 17:20

0 Answers0