Preparation
Before any data manipulation can occur, three (3) new libraries will require installation.
- The Pandas library enables access to/from a DataFrame.
- The Pyarrow library allows writing/reading access to/from a parquet file.
- The Openpyxl library allows styling/writing/reading to/from an Excel file.
To install these libraries, navigate to an IDE terminal. At the command prompt ($
), execute the code below. For the terminal used in this example, the command prompt is a dollar sign ($
). Your terminal prompt may be different.
$ pip install pandas
Hit the <Enter>
key on the keyboard to start the installation process.
$ pip install pyarrow
Hit the <Enter>
key on the keyboard to start the installation process.
$ pip install openpyxl
Hit the <Enter>
key on the keyboard to start the installation process.
If the installations were successful, a message displays in the terminal indicating the same.
Feel free to view the PyCharm installation guide for the required libraries.
Add the following code to the top of each code snippet. This snippet will allow the code in this article to run error-free.
import pandas as pd import pyarrow import openpyxl
DataFrame.to_string()
The to_string()
method converts a DataFrame object to a terminal-based tabbed output.
The syntax for this method is as follows:
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)
The respective parameters:
Parameter | Description |
---|---|
buf | This parameter is the file path/buffer to write. If empty, a string returns. |
columns | This parameter is the sub-set of columns to write. If empty, all columns write. |
col_space | This depicts the length of each column. |
header | This parameter writes out the column names. |
index | This parameter writes out the row (index) names. |
na_rep | This parameter represents the string value for missing data. |
formatters | This parameter is a formatter function to apply to elements by position/name. |
float_format | This parameter is a formatter for floating-point numbers. |
sparsify | If True and MultiIndex, display the key for each row. |
index_names | This parameter displays the index names. |
justify | This parameter determines the column alignment. |
max_rows | This determines the maximum number of rows to display. |
max_cols | This determines the maximum number of columns to display. |
show_dimensions | This parameter displays the dimensions of the DataFrame (total rows/columns). |
decimal | This parameter is the decimal separator, comma (, ) in Europe. |
line_width | This determines the width to wrap a line in characters. |
min_rows | The rows to display if totals rows > max_rows. |
max_colwidth | This determines the maximum width at which to truncate column characters. |
encoding | A string representation of encoding. The default value is UTF-8. |
This example reads in the countries.csv
file to a DataFrame. This DataFrame then converts to a string.
π‘ Note: Click here to save this CSV file. Then move it to the current working directory.
df = pd.read_csv('countries.csv').head(4) result = df.to_string() print(result)
- Line [1] reads in four (4) rows from the
countries.csv
file. The output saves to a DataFramedf
. - Line [2] converts the DataFrame to a string. The output saves to
result
. - Line [3] outputs the result to the terminal.
Output
Country | Capital | Population | Area | |
0 | Germany | Berlin | 83783942 | 357021 |
1 | France | Paris | 67081000 | 551695 |
2 | Spain | Madrid | 47431256 | 498511 |
3 | Italy | Rome | 60317116 | 301338 |
More Pandas DataFrame Methods
Feel free to learn more about the previous and next pandas DataFrame methods (alphabetically) here:
Also, check out the full cheat sheet overview of all Pandas DataFrame methods.