Python | Pandas Series.to_string ()



Series.to_string() Pandas Series.to_string() displays the string representation of the Series.

Syntax: Series.to_string (buf = None, na_rep = `NaN`, float_format = None, header = True, index = True, length = False, dtype = False, name = False, max_rows = None)

Parameter:
buf: buffer to write to
na_rep: string representation of NAN to use, default `NaN`
float_format: formatter function to apply to columns` elements if they are floats default None
header : Add the Series header (index name)
index: Add index (row) labels, default True
length: Add the Series length
dtype: Add the Series dtype
name: Add the Series name if not None
max_rows: Maximum number of rows to show before truncat ing.

Returns: Formatted string.

Example # 1: Using Series.to_string () a function to render the string representation of a given series of objects.

# import pandas as pd

import pandas as pd

 
# Create series

sr = pd.Series ([ `New York` , ` Chicago` , `Toronto` , `Lisbon` , ` Rio` , `Moscow` ])

  
# Create date and time index

didx = pd.DatetimeIndex (start = `2014-08-01 10:00 ` , freq = ` W`

periods = 6 , tz = `Europe / Berlin`

  
# set index

sr.index = didx

  
# Print series

print (sr)

Output:

Now we will use Series.to_string () to render a string representation of this series object.

# render to inline form
sr.to_string ()

Output:

As we can see in the output, Series.to_string () has successfully rendered the string representation for the given object.

Example # 2: Use Series.to_string () to render the string representation of this series object.

# import pandas as pd

import pandas as pd

  
# Create series

sr = pd.Series ([ 19.5 , 16.8 , 22.78 , 20.124 , 18.1002 ])

 
# Print series

print (sr)

Output:

Now we we will use Series.to_string () to render a string representation of this series object.

# render to inline form
sr.to_string ()

Output:

Like us we can see in the output that Series.to_string () has successfully rendered the string representation for the given object.