Home > OS >  pandas dataframe to comma separated string in python
pandas dataframe to comma separated string in python

Time:10-11

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

CodePudding user response:

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
  • Related