Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

pandas

I need to iterate over each row of a pandas df and turn this into a comma separated string.

example:

df3 = DataFrame(np.random.randn(10, 5),               columns=['a', 'b', 'c', 'd', 'e'])             a         b         c         d         e 0 -0.158897 -0.749799  0.268921  0.070035  0.099600 1 -0.863654 -0.086814 -0.614562 -1.678850  0.980292 2 -0.098168  0.710652 -0.456274 -0.373153 -0.533463 3  1.001634 -0.736187 -0.812034  0.223062 -1.337972 4  0.173549 -0.576412 -1.016063 -0.217242  0.443794 5  0.273695  0.335562  0.778393 -0.668368  0.438880 6 -0.783824  1.439888  1.057639 -1.825481 -0.770953 7 -1.025004  0.155974  0.645023  0.993379 -0.812133 8  0.953448 -1.355628 -1.918317 -0.966472 -0.618744 9 -0.479297  0.295150 -0.294449  0.679416 -1.813078 

I'd like to get for each row:

 '-0.158897,-0.749799,0.268921,0.070035,0.099600'  '0.863654,-0.086814,-0.614562,-1.678850,0.980292' ... and so on 
like image 931
Blue Moon Avatar asked Jun 17 '16 09:06

Blue Moon


People also ask

How do I turn a DataFrame row into a list?

The command to convert Dataframe to list is pd. DataFrame. values. tolist().

How do you split rows in a data frame?

Series and DataFrame methods define a . explode() method that explodes lists into separate rows. See the docs section on Exploding a list-like column. Since you have a list of comma separated strings, split the string on comma to get a list of elements, then call explode on that column.

How do you split a row value in Python?

To split cell into multiple rows in a Python Pandas dataframe, we can use the apply method. to call apply with a lambda function that calls str. split to split the x string value. And then we call explode to fill new rows with the split values.

How do you convert a list to a comma separated string in python?

How to Convert a Python List into a Comma-Separated String? You can use the . join string method to convert a list into a string. So again, the syntax is [seperator].


2 Answers

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'] 
like image 183
miradulo Avatar answered Oct 13 '22 04:10

miradulo


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.

like image 26
Dennis Golomazov Avatar answered Oct 13 '22 04:10

Dennis Golomazov