简体   繁体   中英

Convert a series of number become one single line of numbers

If I have a series of numbers in a DataFrame with one column, eg:

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>

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>

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))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM