📜  Python| Pandas DataFrame.to_string(1)

📅  最后修改于: 2023-12-03 15:04:21.693000             🧑  作者: Mango

Python | Pandas DataFrame.to_string

Pandas is an open-source data manipulation and analysis library. It provides various data structures and functions needed to work on structured data seamlessly. One of the most useful functions of Pandas is DataFrame.to_string().

Syntax

Pandas DataFrame.to_string() method has the following syntax.

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, line_width=None, max_rows=None, max_cols=None, show_dimensions=False, decimal='.') -> str
Parameters
  • buf : String buffer or file handle, optional.
  • columns : Index or array-like, optional. This option enables us to select a subset of columns to include in the output. By default, all columns will be outputted.
  • col_space : determines the minimum width of each column, by default 12.
  • header : Boolean, default True. Shows column names if True.
  • index : Boolean, default True. Shows row labels if True.
  • na_rep : String, default "NaN". Use a string to represent NA/NaN values.
  • formatters : Dict of functions for converting values in certain columns. For example, if we want to convert the values in the Name column to uppercase. We could pass the function lambda x: str(x).upper() as a value for the 'Name' key in the formatters dictionary.
  • float_format : Format string for floating-point numbers.
  • sparsify : Boolean, default None. Affects only multi-level index DataFrames. If True, returns a DataFrame with a hierarchical index that omits redundant levels.
  • index_names : Boolean, default True. Shows the index names on the output.
  • justify : Tuple of (left, right) boolean values to justify the field for columns, by default (True, False).
  • line_width : Int, default None. Determines the maximum number of characters per line. If None, the value of pandas.options.display.max_colwidth is used.
  • max_rows : Int, default None. Determines the maximum number of rows to display. If None, Pandas will determine the maximum number of rows that can fit within either the terminal width or pandas.options.display.max_rows.
  • max_cols : Int, default None. Determines the maximum number of columns to display. If None, Pandas will determine the maximum number of columns that can fit within either the terminal width or pandas.options.display.max_columns.
  • show_dimensions : Boolean, default False. Shows the dimensions of the DataFrame (number of rows and columns).
  • decimal – String, default ‘.’. Character recognized as decimal separator.
Returns

Returns a string representation of the DataFrame.

Example

Let's consider an example of how to use the to_string() method.

import pandas as pd

data = {'Name': ['Peter', 'John', 'Sara', 'David'], 'Age': [23, 34, 29, 41], 'City': ['Atlanta', 'Denver', 'Boston', 'Seattle'], 'Salary': [55000, 78000, 92000, 66000]}

df = pd.DataFrame(data)

print(df.to_string())

Output:

    Name  Age      City  Salary
0  Peter   23   Atlanta   55000
1   John   34    Denver   78000
2   Sara   29    Boston   92000
3  David   41   Seattle   66000

This will output the DataFrame with all columns, headers, and row labels.

We can also use to_string() with various other arguments. For example,

print(df.to_string(index=False, header=False))

Output:

 Peter   23   Atlanta   55000
  John   34    Denver   78000
  Sara   29    Boston   92000
 David   41   Seattle   66000

Here, index=False and header=False removes the row labels and column headers.

We can also use to_string() to format the values in the DataFrame. For instance,

df['Name'] = df['Name'].apply(lambda x: str(x).upper())

print(df.to_string(formatters={'Salary': '${:,.2f}'.format}))

Output:

    Name  Age      City      Salary
0  PETER   23   Atlanta   $55,000.00
1   JOHN   34    Denver   $78,000.00
2   SARA   29    Boston   $92,000.00
3  DAVID   41   Seattle   $66,000.00

This example shows how we can format the values in the 'Salary' column to include a dollar sign and commas.

Note: to_string() returns a string representation of the DataFrame. We can assign this string to a variable or write it to a file if needed.