Pandas >> Settings and Options

2022-12-26 Pandas

Table of Contents

This tutorial will introduce some tips about settings and options in Pandas. (version, display options, unicode character)

Pandas

Confirm Pandas Version

version

To check which version of Pandas you have installed, you can use the version attribute of the pandas module. Here’s an example of how to do this in Python:

import pandas as pd

# Print the version of Pandas
print(pd.__version__)

This will print the version of Pandas that you have installed. For example, if you have version 1.1.4 installed, this code will print ‘1.1.4’.

pip freeze

You can also use the !pip freeze command in a Jupyter notebook or the command line to see a list of all the Python packages and their versions that are installed in your environment. For example:

$ pip freeze
numpy==1.19.3
pandas==1.1.4

This will show you the version of Pandas that is installed, as well as the versions of any other Python packages you have installed.

show_versions

You can also use the show_versions function from the pandas.show_versions module to display information about the versions of various software and libraries used by Pandas. This function will display the versions of Pandas, NumPy, and other dependencies, as well as the version of Python and the operating system you are using.

Here’s an example of how to use the show_versions function:

import pandas as pd

pd.show_versions()

This will print the version information to the console. The output will look something like this:

INSTALLED VERSIONS
------------------
commit: None
python: 3.9.0.final.0
python-bits: 64
OS: Windows
OS-release: 10
machine: AMD64
processor: Intel64 Family 6 Model 158 Stepping 10, GenuineIntel
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: en_US.UTF-8

pandas: 1.1.4
pytest: None
pip: 21.0.1
setuptools: 60.1.0
Cython: 0.29.21
numpy: 1.19.3
scipy: 1.5.3
pyarrow: 1.0.1
xarray: None
IPython: 7.19.0
sphinx: 3.7.2
blosc: None
feather: None
pytables: None
tables: None
xlrd: None
xlwt: None
openpyxl: None
lxml: 4.6.1
bs4: 4.9.4
html5lib: 1.1
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: None
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None

This will show you the versions of Pandas, NumPy, and other dependencies that are installed, as well as the version of Python and the operating system you are using.

Change Display Options

To change the display options for Pandas, you can use the set_option function from the pandas module. This function allows you to control the display of various aspects of the DataFrame, such as the number of rows and columns to display, the precision of floating-point numbers, and the maximum width of the output.

Here’s an example of how to use the set_option function to change the display options:

import pandas as pd

# Set the maximum number of rows and columns to display
pd.set_option('display.max_rows', 100)
pd.set_option('display.max_columns', 20)

# Set the precision of floating-point numbers
pd.set_option('display.precision', 2)

# Set the maximum width of the output
pd.set_option('display.width', 120)

These options will affect the way that DataFrames are displayed when you print them to the console. For example, if you have a DataFrame with more than 100 rows or 20 columns, only the first 100 rows or 20 columns will be displayed.

You can also use the reset_option function to reset the display options to their default values. For example:

# Reset all display options to their default values
pd.reset_option('all')

Unicode Characters

To display Unicode characters in a Pandas DataFrame, you can use the unicode_literals module from the future package. This module allows you to use Unicode characters in string literals, which can be useful when displaying data in a DataFrame.

Here’s an example of how to use the unicode_literals module to display Unicode characters in a Pandas DataFrame:

from __future__ import unicode_literals
import pandas as pd

# Create a DataFrame with Unicode characters
df = pd.DataFrame({'Unicode Column': ['\u2665', '\u2666', '\u2663']})

# Display the DataFrame
print(df)

This will output the following DataFrame:

  Unicode Column
0            ♥
1            ♦
2            ♣

Keep in mind that you will need to use the unicode_literals module at the beginning of your script, before you import the pandas module.

The display.unicode.east_asian_width option controls whether or not to use wide characters for East Asian Unicode characters in the output of Pandas. By default, this option is set to True, which means that wide characters will be used for certain East Asian Unicode characters.

For example, consider the following code:

import pandas as pd

# Create a DataFrame with East Asian Unicode characters
df = pd.DataFrame({'East Asian Column': ['\u4e00', '\u4e8c', '\u4e09']})

# Display the DataFrame with the default value for display.unicode.east_asian_width
print(df)

This will output the following DataFrame:

  East Asian Column
0                一
1                二
2                三

Notice that the East Asian Unicode characters are displayed using wide characters.

Subscribe and be the FIRST reader of our latest articles

* indicates required

Contact us