- API reference
- DataFrame
- pandas.DataF...
pandas.DataFrame.to_string#
- DataFrame.to_string(buf=None,*,columns=None,col_space=None,header=True,index=True,na_rep='NaN',formatters=None,float_format=None,sparsify=None,index_names=True,justify=None,max_rows=None,max_cols=None,show_dimensions=False,decimal='.',line_width=None,min_rows=None,max_colwidth=None,encoding=None)[source]#
Render a DataFrame to a console-friendly tabular output.
- Parameters:
- bufstr, Path or StringIO-like, optional, default None
Buffer to write to. If None, the output is returned as a string.
- columnsarray-like, optional, default None
The subset of columns to write. Writes all columns by default.
- col_spaceint, list or dict of int, optional
The minimum width of each column. If a list of ints is given every integers corresponds with one column. If a dict is given, the key references the column, while the value defines the space to use..
- headerbool or list of str, optional
Write out the column names. If a list of columns is given, it is assumed to be aliases for the column names.
- indexbool, optional, default True
Whether to print index (row) labels.
- na_repstr, optional, default ‘NaN’
String representation of
NaNto use.- formatterslist, tuple or dict of one-param. functions, optional
Formatter functions to apply to columns’ elements by position orname.The result of each function must be a unicode string.List/tuple must be of length equal to the number of columns.
- float_formatone-parameter function, optional, default None
Formatter function to apply to columns’ elements if they arefloats. This function must return a unicode string and will beapplied only to the non-
NaNelements, withNaNbeinghandled byna_rep.- sparsifybool, optional, default True
Set to False for a DataFrame with a hierarchical index to printevery multiindex key at each row.
- index_namesbool, optional, default True
Prints the names of the indexes.
- justifystr, default None
How to justify the column labels. If None uses the option fromthe print configuration (controlled by set_option), ‘right’ outof the box. Valid values are
left
right
center
justify
justify-all
start
end
inherit
match-parent
initial
unset.
- max_rowsint, optional
Maximum number of rows to display in the console.
- max_colsint, optional
Maximum number of columns to display in the console.
- show_dimensionsbool, default False
Display DataFrame dimensions (number of rows by number of columns).
- decimalstr, default ‘.’
Character recognized as decimal separator, e.g. ‘,’ in Europe.
- line_widthint, optional
Width to wrap a line in characters.
- min_rowsint, optional
The number of rows to display in the console in a truncated repr(when number of rows is abovemax_rows).
- max_colwidthint, optional
Max width to truncate each column in characters. By default, no limit.
- encodingstr, default “utf-8”
Set character encoding.
- Returns:
- str or None
If buf is None, returns the result as a string. Otherwise returnsNone.
See also
to_htmlConvert DataFrame to HTML.
Examples
>>>d={'col1':[1,2,3],'col2':[4,5,6]}>>>df=pd.DataFrame(d)>>>print(df.to_string()) col1 col20 1 41 2 52 3 6