current position:Home>Pandas table beauty skills

Pandas table beauty skills

2022-02-01 18:40:55 PI dada

official account : Youer cottage
author :Peter
edit :Peter

Hello everyone , I am a Peter~

This article mainly introduces how to beautify Pandas Of DataFrame The data of . Mainly through Pandas Two methods in :

  1. Styler.applymap: Element by element , Return with CSS attribute - value A single string for
  2. Styler.apply: Column 、 surface 、 Row mode , Returns a with the same shape Series perhaps DataFrame, Each of these values is with CSS String of property value pairs . When the method works , Through parameters axis To pass on ,axis=0 Indicates action by column ,axis=1 Indicates action by line .

Official website learning address :

Pandas Serial

Pandas Your article has been serializing :

Analog data

import pandas as pd
import numpy as np

#  Set random seeds , Ensure that the results of each run are the same 

#  Analog data 
df = pd.DataFrame({'A': np.linspace(1, 10, 10)})
#  Put two DataFrame A merger 
df1 = pd.concat([df, pd.DataFrame(np.random.randn(10,4), columns=list("BCDE"))],axis=1)
 Copy code 

numpy Medium linspace(start,stop,setp): Indicates that the data is retrieved according to the step size , Data including head and tail :

To see the effect of null values , Specially set 3 A null value nan:

View and hide styles

The first is to look at DataFrame Style settings for :

We can go through render Method to view the specific value of the data frame style : Discovery is the default CSS Style code

Hide index

hide index Code for :

Hides the specified column

We found that AC Two columns are hidden , The parameters used are subset Parameters :

The following are examples of various display styles :

Case study 1: Positive and negative colors

Less than 0 Show red , Greater than 0 Show blue

def color_change(val):
    color = 'red' if val < 0 else 'blue'
    return 'color: %s' % color  
 Copy code 
#  Use applymap And call the written function
 Copy code 

applymap The method is for the whole DataFrame Of

Case study 2: Highlight data

You can highlight the maximum 、 Minimum and missing values . Handwriting a simple highlight function :

def highlight_min(x):
    is_min = x == x.min()
    return ["background-color: yellow" if v else '' for v in is_min]
 Copy code 

example 3: Use the default highlight function

Pandas Built in... Has been written by default 3 A highlight function :

  • highlight_max(): Maximum
  • highlight_min(): Minimum
  • highlight_null(): Null value

At the same time, we can also match parameters axis To display rows or columns

1、 Highlight maximum

2、 Highlight the minimum value of each column

3、 Highlight null : The default is red

Of course, we can change the color , By using parameters null_color:

Using parameter axis=1, Indicates the operation in the direction of the row :

Case study 4: call chaining

Chained calls refer to the simultaneous use of multiple functions in the same style operation ( Method ):

# color_change + highlight_max
 Copy code 

If there are many methods , You can write :

# color_change + highlight_max\

#  perhaps : Add a layer of parentheses outside , Indicates that the whole code is a whole 
 Copy code 

Null highlighting and changing color parameters are used at the same time , Implement chain call :

Hide column properties and custom parameters at the same time :

example 5: Part of the data is beautified

Using parameter subset Parameter controls the action of the row and column , The parameter passed in can be :

  • Column labels
  • list (numpy Array )
  • Tuples (row_indexer, column_indexer)

Some column properties

Through parameters subset To specify the column properties we want to beautify :

pd.IndexSlice Construct a list

adopt pd.IndexSlice To construct the

example 6: Formatted display of values (Styler.format)

Styler.format To control the formatted output of values , Be similar to Python String formatting in

Overall display

For example, we want all data to display only two decimal places :

Using dictionaries , Format specific columns

In the form of a dictionary , Use the column attribute name as the key , Format description as value , Make formatting settings :

Use lambda Anonymous functions

A null value displays

Through parameters na_rep, Replace null values :

use “ Null value ” Two words to replace :

Chain calls are used at the same time “-” To replace :


Use emoji Emoticons :

example 7: Built in style

Built in functions

The built-in null highlighting function is used here :

Thermal map making

We use seaborn Library to implement :

import seaborn as sns

cm = sns.light_palette("blue", as_cmap=True)
s =
 Copy code 

Continuous chromatography

Through parameters low and high To specify the range of Chromatography :

set_properties Use

### set_properties Use**{"background":"yellow",  #  Background color 
                           "color":"red",  #  typeface 
                           "border-color":"black"})  #  The border 
 Copy code 

example 8: Personalized bar chart

Default mode

adopt bar Method to operate :

Parameters align

Yes 3 There are two kinds of alignment :

  • left: The minimum value starts from the left side of the cell
  • zero: The zero value is in the center of the cell
  • mid: The center of the cell is (max-min)/2 It's about

example 9: Style sharing

Suppose one of us DataFrame Created a style , Then I want to be in another DataFrame Use this style directly in , How do you do that ?

Let's create a style style1:

take style1 Apply to style2 in :

example 10: Setting accuracy set_precision

It is usually to assign precision to the data in the data frame ( Decimal places ):

example 11: Set title

Use set_caption Method

Missing value setting

It uses set_na_rep function :

    .format(None, na_rep="PASS", subset=["D"])
 Copy code 

example 13: Comprehensive case

  .set_na_rep('-')  #  Set null value 
  .format({'C':'{:.4f}',  #  Precision setting 
           'E':'{:.2%}'},na_rep="-")  #  Precision and null values are set at the same time 
  .highlight_null("green")  #  Null values highlight 
  .applymap(color_change,subset=['D'])  # D Column usage color_change function 
  .apply(highlight_min,subset=['C'])  # C Column usage highlight_min
  .background_gradient(cmap='cubehelix',subset=['B','D'])  #  Background color settings 
  .bar(subset=['E'], align='mid', color=['#60BCD4','#6A1B9A'])  #  Histogram settings 
  .set_caption("Title of Pandas Style")  #  Title Setting 
  .hide_index()  #  Hide index 
  .hide_columns(subset=['A']))  #  hide A Column 
 Copy code 

example 14: The ultimate weapon

#  Version requires at least pandas1.2.0
style1 = [
    dict(selector="th", props=[("font-size", "125%"), 
                               ("text-align", "center"),
                               ("background-color", "#F0F3CF"),
    dict(selector="td", props=[("font-size", "105%"), 
                               ("text-align", "right"),
    dict(selector="caption", props=[("caption-side", "top"),
                                    ("text-align", "left"),

style2 = {
    'A': [dict(selector='td', props=[('text-align','center'),
    'B': [dict(selector='td', props=[('text-align','left'),
    'C': [dict(selector='td', props=[('text-decoration','underline'),
#  Chain call various setting methods 
  .set_na_rep('-')  #  Overall null value 
  .format({'C':'{:.4f}','D':'{:.4f}','E':'{:.2%}'},na_rep="-")  #  precision 
  .highlight_null("gray")  #  Highlight null 
  .applymap(color_change,subset=['B']).highlight_max(subset=['A'])  # color_change Function and maximum highlight 
  .background_gradient(cmap='Pastel1',subset=['C','D'])  #  background 
  .bar(subset=['E'], align='mid', color=['#90BCD4','#6A1B9A'])  #  Columnar 
  .set_caption("Advanced use of Pandas Style")  #  title  
 	.hide_index()  #  Hide index 
  .hide_columns(subset=['E'])  #  hide E Column  
  .set_table_styles(style1).set_table_styles(style2,overwrite=False)  #  Style transfer 
  .set_properties(**{'font-family': 'Microsoft Yahei','border-collapse': 'collapse',  #  Property settings 
                     'border-top': '1px solid black','border-bottom': '1px solid black'}))
 Copy code 

Output to Excel

This is a feature that is still in development , take DataFrame Use openyxl perhaps xlswriter Export as an engine to Excel In the table , That's what the official website says :

Here's a simple case :

   .applymap(color_change)  #  Positive and negative change color 
   .apply(highlight_min)  #  Highlight minimum 
   .to_excel('styled.xlsx', engine='openpyxl'))
 Copy code 

If we don't want index numbers , add to index=False:

   .applymap(color_change)  #  Positive and negative change color 
   .apply(highlight_min)  #  Highlight minimum 
   .to_excel('styled.xlsx', engine='openpyxl', index=False))
 Copy code 

copyright notice
author[PI dada],Please bring the original link to reprint, thank you.

Random recommended