Public account: You and the cabin by: Peter Editor: Peter

Hello, I’m Peter

In Pandas, we work with tables in addition to data. In order to present a table data well, you must have good setup up front.

This article describes how to configure Pandas. Options and setings are used for Pandas. Strong push official website learning address: pandas.pydata.org/pandas-docs…

The import

This is the import way of a kind of international convention!

import pandas as pd
Copy the code

Ignore the warning

Some of the uses of Pandas may be removed in the near future due to an update to the software version. Warnings will be ignored by inserting the following code:

# ignore warnings
import warnings
warnings.filterwarnings('ignore')
Copy the code

Float data precision

Viewing the default precision

The default is to keep 6 decimal places. Print the current precision as follows:

pd.get_option( 'display.precision')
Copy the code
6
Copy the code

Modify the accuracy

Set the accuracy to 2 bits

Pd. Set_option (' display. Precision, 2) # 2: writing pd. The options. The display. The precision = 2Copy the code

Then we print again and the current precision is 2 bits:

pd.get_option( 'display.precision')
Copy the code
2
Copy the code

The lines

View the number of rows displayed

The default number of rows displayed is 60

Pd.get_option ("display.max_rows") # Default is 60Copy the code
60
Copy the code

The minimum number of rows is 10 by default:

Pd.get_option ("display.min_rows") # Minimum rows to displayCopy the code
10
Copy the code

Modify the number of lines displayed

Change the maximum number of lines displayed to 999 and then look again:

Pd.set_option ("display.max_rows",999) # Maximum number of rows to displayCopy the code
pd.get_option("display.max_rows")
Copy the code
999
Copy the code

Change the minimum number of lines displayed:

pd.set_option("display.min_rows",20)  
Copy the code
pd.get_option("display.min_rows")
Copy the code
20
Copy the code

The reset function

With the reset reset_option method, the setting becomes the default form (numeric) :

pd.reset_option("display.max_rows")
Copy the code
Pd.get_option ("display.max_rows") # revert to 60Copy the code
60
Copy the code
pd.reset_option("display.min_rows")
Copy the code
Pd.get_option ("display.min_rows") # revert to 10Copy the code
10
Copy the code

A regular function

If we modify multiple options and want to restore them at the same time, we can use regular expressions to reset multiple options.

Reset all Settings starting with displacy:

Pd. reset_option("^display")Copy the code

Reset all

If all is used, all Settings are reset:

pd.reset_option('all')
Copy the code

According to the column

If you can control the number of rows displayed, you can also control the number of columns displayed

View the number of columns displayed

The default number of columns displayed is 20:

Pd.get_option ('display.max_columns') # alternative: pd.options.display.max_columnsCopy the code
20
Copy the code

Change the number of columns

Change the number of columns displayed to 100:

# change to 100 pd.set_option('display.max_columns',100)Copy the code

View the modified column number:

Pd.get_option ('display.max_columns')Copy the code
100
Copy the code

Display all columns

If set to None, all columns are displayed:

pd.set_option('display.max_columns',None)
Copy the code

reset

pd.reset_option('display.max_columns')
Copy the code

Modify the column width

View the number of columns above, and set the width for each column below. The width of a single column of data, measured in the number of characters, indicated by an ellipsis when exceeded.

The default column width

The default column width is 50 characters wide:

pd.get_option ('display.max_colwidth')
Copy the code
50
Copy the code

Modify the column width

Modify display column width to 100:

Pd. set_option ('display.max_colwidth', 100)Copy the code

View the column width lengths displayed:

pd.get_option ('display.max_colwidth')
Copy the code
100
Copy the code

Display all columns

Display all columns:

pd.set_option ('display.max_colwidth', None)
Copy the code

Fold function

When we output the width of the data beyond the set width, whether to fold. Usually False does not fold, and True does the opposite.

Pd.set_option ("expand_frame_repr", True) # foldCopy the code
Pd.set_option ("expand_frame_repr", False) # do not foldCopy the code

Code snippets modify Settings

All of the Settings described above, if modified, are environmental-wide; We can also make only temporary Settings for a block of code.

Running out of the current code block will fail and revert to the original Settings.

Suppose this is the first code block:

print(pd.get_option("display.max_rows"))
print(pd.get_option("display.max_columns"))
60
20
Copy the code

Here is the second code block:

The current code block is set

with pd.option_context("display.max_rows".20."display.max_columns".10) :print(pd.get_option("display.max_rows"))
    print(pd.get_option("display.max_columns"))
20
10
Copy the code

Here’s the third code block:

print(pd.get_option("display.max_rows"))
print(pd.get_option("display.max_columns"))
60
20
Copy the code

As you can see from the example above, the Settings are invalid outside the specified code block

Number formatting

Pandas has a display.float_format method that formats the output of floating point numbers in thousandths, percentages, fixed decimal places, etc.

This method can also be used if other data types can be converted to floating point numbers.

The callable should accept a floating point number and return a string with the desired format of the number

In the thousandths place

When the data is large, we want to express the data in the form of thousandths, which is clear at a glance:

Df = pd DataFrame ({" percent ": [12.98, 6.13, 7.4]," number ": [1000000.3183, 2000000.4578, 3000000.2991]}) dfCopy the code

The percentage

Special symbols

In addition to the % sign, we can use other special symbols:

Zero threshold conversion

What does threshold conversion mean? The first implementation of this function uses the display.chop_threshold method.

Represents the threshold for displaying data in Series or DF as a number. If the value is greater than this, it is displayed directly; If less than, use 0.

Change drawing method

By default, pandas uses the matplotlib as the drawing backend. We can modify the Settings:

Import matplotlib.pyplot as PLT %matplotlib inline # default df1 = pd.DataFrame(dict(a=[5,3,2], B =,4,1 [3])) df1. The plot (kind = "bar") PLT. The show ()Copy the code

Change the drawing backend to a powerful Plotly:

Writing # 1
pd.options.plotting.backend = "plotly"

df = pd.DataFrame(dict(a=[5.3.2], b=[3.4.1]))
fig = df.plot()
fig.show()

# write 2
df = pd.DataFrame(dict(a=[5.3.2], b=[3.4.1]))
fig = df.plot(backend='plotly') # specify it here
fig.show()
Copy the code

Modify column header alignment

By default, the property fields (column headers) are aligned to the right, which we can set. Here’s an example from the official website:

Prints out the current Settings and resets all options

Pd.describe_option () prints all the current Settings and recharges all options. Here are some of the options:

Configuration tips

The following is a summary of common configurations that can be used by copying:

import pandas as pd  # International practice

import warnings
warnings.filterwarnings('ignore')  # Ignore the warning

pd.set_option( 'display.precision'.2)
pd.set_option("display.max_rows".999)  # maximum number of rows to display
pd.set_option("display.min_rows".20)   Minimum number of rows to display
pd.set_option('display.max_columns'.None)  # all columns
pd.set_option ('display.max_colwidth'.100)   Change the column width
pd.set_option("expand_frame_repr".True)  # folding
pd.set_option('display.float_format'.'{:,.2f}'.format)  # micrometer
pd.set_option('display.float_format'.'{:.2f}%'.format)  # Percentage form
pd.set_option('display.float_format'.'{: 2 f} $'.format)  # special symbol
pd.options.plotting.backend = "plotly"  # Modify drawing
pd.set_option("colheader_justify"."left")  # column field alignment
pd.reset_option('all')  # reset
Copy the code