Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Convert categories to binary columns (concat the category columns)

Want to convert the categories to binary columns, concatenated to the df. Category column values should be new columns with 0 or 1s for each id based on if the value is present or not.

df = pd.DataFrame({"id": [0,1,1,3,3],
                     "value1": ["ryan", "delta", "delta", "delta", "alpha"], 
                     "category": ["teacher", "pilot", "engineer", "pilot", "teacher"], 
                     "value2": [1, 1, 2, 3, 7]})
df

Answer df should be:

finaldf = pd.DataFrame({"id": [0,1,3],
                       "teacher":[1,0,1],
                       "pilot":[0,1,1],
                       "engineer": [0,1,0]})

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Use pd.get_dummies:

finaldf = pd.get_dummies(df, columns=["category"], prefix="", prefix_sep="")

output:

     value1  value2  engineer  pilot  teacher
0  0   ryan       1         0      0        1
1  1  delta       1         0      1        0
2  2  delta       2         1      0        0
3  3  delta       3         0      1        0

Edit to new question 😉

Use pd.crosstab with some additional cleaning:

finaldf = pd.crosstab(df["id"], df["category"]).reset_index().rename_axis(columns=None)

output:

   id  engineer  pilot  teacher
0   0         0      0        1
1   1         1      1        0
2   3         0      1        1
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading