pandas dataframe to comma separated string in python

Question:

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

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

How can I do that? Please help me

Asked By: duy dang

||

Answers:

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
Answered By: Jason Baker
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.