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

Joining values from a DataFrame row with comma being separator

My DataFrame row line = fd.iloc[0] is:

id                         31458598
total_matched             104671.25
market           Under 3.5 Goals FT
one                               0
two                               0
three                             0
four                              0
five                              0
Name: 0, dtype: object

I have join this values with comma, like this:

f"{line['id']},{line['total_matched']},{line['market']},{line['one']},{line['two']},{line['three']},{line['four']},{line['five']}\n"

But this method is archaic, so I tried to use join:

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

','.join(line) + '\n'
TypeError: sequence item 0: expected str instance, numpy.int64 found
line.join(',') + '\n'
AttributeError: 'Series' object has no attribute 'join'

What is the correct way to join these values with a comma?

>Solution :

You can use str.cat after ensuring you have strings:

line.astype(str).str.cat(sep=',')

output: '31458598,104671.25,Under 3.5 Goals FT,0,0,0,0,0,0'

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