close

How to turn a pandas dataframe row into a comma separated string

Hello Guys, How are you all? Hope You all Are Fine. Today We Are Going To learn about How to turn a pandas dataframe row into a comma separated string in Python. So Here I am Explain to you all the possible Methods here.

Without wasting your time, Let’s start This Article.

Table of Contents

How to turn a pandas dataframe row into a comma separated string?

  1. How to turn a pandas dataframe row into a comma separated string?

    You could use pandas.DataFrame.to_string with some optional arguments set to False and then split on newline characters to get a list of your strings. This feels a little dirty though.

  2. turn a pandas dataframe row into a comma separated string

    You could use pandas.DataFrame.to_string with some optional arguments set to False and then split on newline characters to get a list of your strings. This feels a little dirty though.

Method 1

You could use pandas.DataFrame.to_string with some optional arguments set to False and then split on newline characters to get a list of your strings. This feels a little dirty though.

x = df3.to_string(header=False,
                  index=False,
                  index_names=False).split('\n')
vals = [','.join(ele.split()) for ele in x]
print(vals)

Outputs:

['1.221365,0.923175,-1.286149,-0.153414,-0.005078', '-0.231824,-1.131186,0.853728,0.160349,1.000170', '-0.147145,0.310587,-0.388535,0.957730,-0.185315', '-1.658463,-1.114204,0.760424,-1.504126,0.206909', '-0.734571,0.908569,-0.698583,-0.692417,-0.768087', '0.000029,0.204140,-0.483123,-1.064851,-0.835931', '-0.108869,0.426260,0.107286,-1.184402,0.434607', '-0.692160,-0.376433,0.567188,-0.171867,-0.822502', '-0.564726,-1.084698,-1.065283,-2.335092,-0.083357', '-1.429049,0.790535,-0.547701,-0.684346,2.048081']

Method 2

Use to_csv:

df = pd.DataFrame(np.random.randn(10, 5),
                  columns=['a', 'b', 'c', 'd', 'e'])
df.to_csv(header=None, index=False).strip('\n').split('\n')

['-1.60092768589,-0.746496859432,0.662527724304,-0.677984969682,1.70656657572',
 '-0.432306620615,-0.396499851892,0.564494290965,-1.01196068617,-0.630576490671',
 '-3.28916785414,0.627240166663,-0.359262938883,0.344156143177,-0.911269843378',
 '-0.272741450301,0.0594234886507,-2.72800253986,-0.821610087419,-0.0668212419497',
 '0.303490090149,-1.61344483051,0.117046351282,-1.46936429231,-0.66018613208',
 '-1.18157229705,-0.766519504863,0.386180129978,0.945274532852,-0.783459830884',
 '-1.27118723107,-1.12478330038,-0.625470220821,-0.453053132109,0.0641830786961',
 '-1.02657336234,-1.01556460318,0.445282883845,0.589873985417,-0.833648685855',
 '0.742343897524,-1.69644542886,-1.03886940911,0.511317569685,1.87084848086',
 '-0.159125435887,1.02522202275,0.254459603867,-0.487187861352,2.31900012693']

Note: this needs to be improved if you have \n in your cells.

Summery

It’s all About this issue. Hope all Methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which Method worked for you? Thank You.

Also, Read