Home > Enterprise >  Convert a series of number become one single line of numbers
Convert a series of number become one single line of numbers

Time:02-01

If I have a series of numbers in a DataFrame with one column, e.g.:

import pandas as pd

data = [4, 5, 6, 7, 8, 9, 10, 11]
pd.DataFrame(data)

Which looks like this (left column = index, right column = data):

0  4
1  5
2  6
3  7 
4  8
5  9
6  10
7  11

How do I make it into one sequence number, so (4 5 6 7 8 9 10 11) in python or pandas ? because i want to put that into xml file so it looks like this

  <Or>
   <numbers>
     <example>4 5 6 7 8 9 10 11</example>
   </numbers>
  </Or>

CodePudding user response:

You can use a f-string with conversion of the integers to string and str.join:

text = f'''  <Or>
   <numbers>
     <example>{" ".join(s.astype(str))}</example>
   </numbers>
  </Or>'''

Output:

  <Or>
   <numbers>
     <example>4 5 6 7 8 9 10 11</example>
   </numbers>
  </Or>

CodePudding user response:

use tolist() to convert the column value to list and then you can join the list with str(...)

marks_list = (df['Marks'].tolist())
  
marks_str = (' '.join(str(x) for x in marks_list))
  • Related