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

pandas dataframe to comma separated string in python

I have a data frame:

id      name    color   age
1     john    red     20
2     smith   blue    30
3     zang    green   50

I want to get the 3th row in string with format:

id,name,color,age

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

1,"join","red","20"

How can I do that? Please help me

>Solution :

Can’t tell if you want the first or third so here is both.

my_str = df[df.columns].astype(str).apply(lambda x: ", ".join(x), axis=1)[0]
print(my_str)

1, john, red, 20

my_str = df[df.columns].astype(str).apply(lambda x: ", ".join(x), axis=1)[2]
print(my_str)

3, zang, green, 50
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