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 python list into column headers and perform count

I have currently a dataframe which looks like this and I have done a df["Keywords"] = df["Keywords"].str.split(" ")

ID | Keywords
1 | [agile]
2 | [python, python, python] 
3 | [agile, agile]

What I would like to achieve is something like this in which I tranpose the values into column headers and do a count. I tried to expand=true but that didnt work. Is there anyway I can achieve this?

ID | agile | python
1 | 1 | 0
2 | 0 | 1
3 | 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 :

Don’t split.. you can directly encode the values using get_dummies

df[['ID']].join(df['Keywords'].str.get_dummies(sep=' '))
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