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 :
- Styler.applymap: Element by element , Return with CSS attribute - value A single string for
- 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
np.random.seed(20)
# 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)
df1
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
df1.style.applymap(color_change)
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
df1.style.applymap(color_change).apply(highlight_min)
Copy code
If there are many methods , You can write :
# color_change + highlight_max
df1.style.\
applymap(color_change).\
apply(highlight_min)
# perhaps : Add a layer of parentheses outside , Indicates that the whole code is a whole
(df1.style.
applymap(color_change).
apply(highlight_min))
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 :
Emoticons
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 = df1.style.background_gradient(cmap=cm)
s
Copy code
Continuous chromatography
Through parameters low and high To specify the range of Chromatography :
set_properties Use
### set_properties Use
df1.style.set_properties(**{"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 :
(df1.style.set_na_rep("FAIL")
.format(None, na_rep="PASS", subset=["D"])
.highlight_null("yellow"))
Copy code
example 13: Comprehensive case
(df1.style
.set_na_rep('-') # Set null value
.format({'C':'{:.4f}', # Precision setting
'D':'{:.4f}',
'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"),
('width',"100px"),
('height','80px')]),
dict(selector="td", props=[("font-size", "105%"),
("text-align", "right"),
('width',"150px"),
('height','50px')]),
dict(selector="caption", props=[("caption-side", "top"),
("font-size","150%"),
("font-weight","bold"),
("text-align", "left"),
('height','50px'),
('color','#E74C3C')])]
style2 = {
'A': [dict(selector='td', props=[('text-align','center'),
("font-weight","bold"),
("text-transform","capitalize")])],
'B': [dict(selector='td', props=[('text-align','left'),
("font-style","italic")])],
'C': [dict(selector='td', props=[('text-decoration','underline'),
('text-decoration-color','red'),
('text-decoration-style','wavy')])]}
# Chain call various setting methods
(df1.style
.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 :
(df1.style
.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:
(df1.style
.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.
https://en.pythonmana.com/2022/02/202202011840538526.html
The sidebar is recommended
- Python data analysis - linear regression selection fund
- How to make a python SDK and upload and download private servers
- Python from 0 to 1 (day 20) - basic concepts of Python dictionary
- Django -- closure decorator regular expression
- Implementation of home page and back end of Vue + Django tourism network project
- Easy to use scaffold in Python
- [Python actual combat sharing] I wrote a GIF generation tool, which is really TM simple (Douluo continent, did you see it?)
- [Python] function decorators and common decorators
- Explain the python streamlit framework in detail, which is used to build a beautiful data visualization web app, and practice making a garbage classification app
- Construction of the first Django project
guess what you like
-
Python crawler actual combat, pyecharts module, python realizes the visualization of river review data
-
Python series -- web crawler
-
Plotly + pandas + sklearn: shoot the first shot of kaggle
-
How to learn Python systematically?
-
Analysis on several implementations of Python crawler data De duplication
-
leetcode 1616. Split Two Strings to Make Palindrome (python)
-
Python Matplotlib drawing violin diagram
-
Python crawls a large number of beautiful pictures with 10 lines of code
-
[tool] integrated use of firebase push function in Python project
-
How to use Python to statistically analyze access logs?
Random recommended
- How IOS developers learn Python Programming 22 - Supplement 1
- Python can meet any API you need
- Python 3 process control statement
- The 20th of 120 Python crawlers, 1637. All the way business opportunity network joined in data collection
- Datetime of pandas time series preamble
- How to send payslips in Python
- [Python] closure and scope
- Application of Python Matplotlib color
- leetcode 1627. Graph Connectivity With Threshold (python)
- Python thread 08 uses queues to transform the transfer scenario
- Python: simple single player strange game (text)
- Daily python, chapter 27, Django template
- TCP / UDP communication based on Python socket
- Use of pandas timestamp index
- leetcode 148. Sort List(python)
- Confucius old book network data collection, take one anti three learning crawler, python crawler 120 cases, the 21st case
- [HTB] cap (datagram analysis, setuid capability: Python)
- How IOS developers learn Python Programming 23 - Supplement 2
- How to automatically identify n + 1 queries in Django applications (2)?
- Data analysis starts from scratch. Pandas reads HTML pages + data processing and analysis
- 1313. Unzip the coding list (Java / C / C + + / Python / go / trust)
- Python Office - Python edit word
- Collect it quickly so that you can use the 30 Python tips for taking off
- Strange Python strip
- Python crawler actual combat, pyecharts module, python realizes China Metro data visualization
- DOM breakpoint of Python crawler reverse
- Django admin custom field stores links in the database after uploading files to the cloud
- Who has powder? Just climb who! If he has too much powder, climb him! Python multi-threaded collection of 260000 + fan data
- Python Matplotlib drawing streamline diagram
- The game comprehensively "invades" life: Python releases the "cool run +" plan!
- Python crawler notes: use proxy to prevent local IP from being blocked
- Python batch PPT to picture, PDF to picture, word to picture script
- Advanced face detection: use Dlib, opencv and python to detect face markers
- "Python 3 web crawler development practice (Second Edition)" is finally here!!!!
- Python and Bloom filters
- Python - singleton pattern of software design pattern
- Lazy listening network, audio novel category data collection, multi-threaded fast mining cases, 23 of 120 Python crawlers
- Troubleshooting ideas and summary of Django connecting redis cluster
- Python interface automation test framework (tools) -- interface test tool requests
- Implementation of Morse cipher translator using Python program