Phần này thể hiện trực quan hóa dữ liệu bảng bằng cách sử dụng lớp Styler. Để biết thông tin về trực quan hóa với biểu đồ, vui lòng xem trực quan biểu đồ. Tài liệu này được viết dưới dạng máy tính xách tay Jupyter và có thể được xem hoặc tải xuống tại đây.Styler class. For information on visualization with charting please see Chart Visualization. This document is written as a Jupyter Notebook, and can be viewed or downloaded here.
Đối tượng Styler và HTML#
Kiểu dáng nên được thực hiện sau khi dữ liệu trong một khung dữ liệu đã được xử lý. Nhà tạo mẫu tạo ra một HTML
s.set_table_styles[[ {'selector': 'th.col_heading', 'props': 'text-align: center;'}, {'selector': 'th.col_heading.level0', 'props': 'font-size: 1.5em;'}, {'selector': 'td', 'props': 'text-align: center; font-weight: bold;'}, ], overwrite=False]6 và tận dụng ngôn ngữ tạo kiểu CSS để thao tác nhiều tham số bao gồm màu sắc, phông chữ, biên giới, nền, v.v. Xem ở đây để biết thêm thông tin về các bảng HTML kiểu dáng. Điều này cho phép rất nhiều sự linh hoạt ra khỏi hộp và thậm chí cho phép các nhà phát triển web tích hợp DataFrames vào các thiết kế giao diện người dùng thoát của họ.Styler creates an HTML
s.set_table_styles[[ {'selector': 'th.col_heading', 'props': 'text-align: center;'}, {'selector': 'th.col_heading.level0', 'props': 'font-size: 1.5em;'}, {'selector': 'td', 'props': 'text-align: center; font-weight: bold;'}, ], overwrite=False]6 and leverages CSS styling language to manipulate many parameters including colors, fonts, borders, background, etc. See here for more information on styling HTML tables. This allows a lot of flexibility out of the box, and even enables web developers to integrate DataFrames into their exiting user interface designs.
Thuộc tính
s.set_table_styles[[ {'selector': 'th.col_heading', 'props': 'text-align: center;'}, {'selector': 'th.col_heading.level0', 'props': 'font-size: 1.5em;'}, {'selector': 'td', 'props': 'text-align: center; font-weight: bold;'}, ], overwrite=False]7 là một thuộc tính trả về một đối tượng Styler. Nó có một phương thức
s.set_table_styles[[ {'selector': 'th.col_heading', 'props': 'text-align: center;'}, {'selector': 'th.col_heading.level0', 'props': 'font-size: 1.5em;'}, {'selector': 'td', 'props': 'text-align: center; font-weight: bold;'}, ], overwrite=False]8 được xác định trên đó để chúng được hiển thị tự động trong sổ ghi chép Jupyter.Styler object. It has a
s.set_table_styles[[ {'selector': 'th.col_heading', 'props': 'text-align: center;'}, {'selector': 'th.col_heading.level0', 'props': 'font-size: 1.5em;'}, {'selector': 'td', 'props': 'text-align: center; font-weight: bold;'}, ], overwrite=False]8 method defined on it so they are rendered automatically in Jupyter Notebook.
import pandas as pd import numpy as np import matplotlib as mpl df = pd.DataFrame[[[38.0, 2.0, 18.0, 22.0, 21, np.nan],[19, 439, 6, 452, 226,232]], index=pd.Index[['Tumour [Positive]', 'Non-Tumour [Negative]'], name='Actual Label:'], columns=pd.MultiIndex.from_product[[['Decision Tree', 'Regression', 'Random'],['Tumour', 'Non-Tumour']], names=['Model:', 'Predicted:']]] df.style
38.000000 | 2.000000 | 18.000000 | 22.000000 | 21 | nan |
19.000000 | 439.000000 | 6.000000 | 452.000000 | 226 | 232.000000 |
Đầu ra ở trên trông rất giống với biểu diễn HTML DataFrame tiêu chuẩn. Nhưng HTML ở đây đã gắn một số lớp CSS vào mỗi ô, ngay cả khi chúng tôi chưa tạo ra bất kỳ phong cách nào. Chúng ta có thể xem chúng bằng cách gọi phương thức .to_html [], trả về HTML RAW dưới dạng chuỗi, rất hữu ích để xử lý hoặc thêm vào một tệp - đọc thêm về CSS và HTML. Dưới đây chúng tôi sẽ chỉ ra cách chúng tôi có thể sử dụng chúng để định dạng DataFrame để giao tiếp nhiều hơn. Ví dụ: làm thế nào chúng ta có thể xây dựng
s.set_table_styles[[ {'selector': 'th.col_heading', 'props': 'text-align: center;'}, {'selector': 'th.col_heading.level0', 'props': 'font-size: 1.5em;'}, {'selector': 'td', 'props': 'text-align: center; font-weight: bold;'}, ], overwrite=False]9:.to_html[] method, which returns the raw HTML as string, which is useful for further processing or adding to a file - read on in More about CSS and HTML. Below we will show how we can use these to format the DataFrame to be more communicative. For example how we can build
s.set_table_styles[[ {'selector': 'th.col_heading', 'props': 'text-align: center;'}, {'selector': 'th.col_heading.level0', 'props': 'font-size: 1.5em;'}, {'selector': 'td', 'props': 'text-align: center; font-weight: bold;'}, ], overwrite=False]9:
Ma trận nhầm lẫn cho nhiều mô hình dự đoán ung thư.
38 | 2 | 18 | 22 |
19 | 439 | 6 | 452 |
Đầu ra ở trên trông rất giống với biểu diễn HTML DataFrame tiêu chuẩn. Nhưng HTML ở đây đã gắn một số lớp CSS vào mỗi ô, ngay cả khi chúng tôi chưa tạo ra bất kỳ phong cách nào. Chúng ta có thể xem chúng bằng cách gọi phương thức .to_html [], trả về HTML RAW dưới dạng chuỗi, rất hữu ích để xử lý hoặc thêm vào một tệp - đọc thêm về CSS và HTML. Dưới đây chúng tôi sẽ chỉ ra cách chúng tôi có thể sử dụng chúng để định dạng DataFrame để giao tiếp nhiều hơn. Ví dụ: làm thế nào chúng ta có thể xây dựng s.set_table_styles[[
{'selector': 'th.col_heading', 'props': 'text-align: center;'},
{'selector': 'th.col_heading.level0', 'props': 'font-size: 1.5em;'},
{'selector': 'td', 'props': 'text-align: center; font-weight: bold;'},
], overwrite=False]
9:
Ma trận nhầm lẫn cho nhiều mô hình dự đoán ung thư.
& nbsp;Styler can distinguish the display value from the actual value, in both datavalues and index or columns headers. To control the display value, the text is printed in each cell as string, and we can use the .format[] and .format_index[] methods to manipulate this according to a format spec string or a callable that takes a single value and returns a string. It is possible to define this for the whole table, or index, or for individual columns, or MultiIndex levels.
Định dạng màn hình#precision argument to specifically help formatting floats, as well as decimal and thousands separators to support other locales, an na_rep argument to display missing data, and an escape argument to help displaying safe-HTML or safe-LaTeX. The default formatter is configured to adopt pandas’
s.set_table_styles[{ ['Regression', 'Tumour']: [{'selector': 'th', 'props': 'border-left: 1px solid white'}, {'selector': 'td', 'props': 'border-left: 1px solid #000066'}] }, overwrite=False, axis=0]0 option, controllable using
s.set_table_styles[{ ['Regression', 'Tumour']: [{'selector': 'th', 'props': 'border-left: 1px solid white'}, {'selector': 'td', 'props': 'border-left: 1px solid #000066'}] }, overwrite=False, axis=0]1
df.style.format[precision=0, na_rep='MISSING', thousands=" ", formatter={['Decision Tree', 'Tumour']: "{:.2f}", ['Regression', 'Non-Tumour']: lambda x: "$ {:,.1f}".format[x*-1e6] }]
38.00 | 2 | 18 | nan | 21 | Không phải khối u [tiêu cực] |
19.00 | 439 | 6 | Đầu ra ở trên trông rất giống với biểu diễn HTML DataFrame tiêu chuẩn. Nhưng HTML ở đây đã gắn một số lớp CSS vào mỗi ô, ngay cả khi chúng tôi chưa tạo ra bất kỳ phong cách nào. Chúng ta có thể xem chúng bằng cách gọi phương thức .to_html [], trả về HTML RAW dưới dạng chuỗi, rất hữu ích để xử lý hoặc thêm vào một tệp - đọc thêm về CSS và HTML. Dưới đây chúng tôi sẽ chỉ ra cách chúng tôi có thể sử dụng chúng để định dạng DataFrame để giao tiếp nhiều hơn. Ví dụ: làm thế nào chúng ta có thể xây dựng s.set_table_styles[[ {'selector': 'th.col_heading', 'props': 'text-align: center;'}, {'selector': 'th.col_heading.level0', 'props': 'font-size: 1.5em;'}, {'selector': 'td', 'props': 'text-align: center; font-weight: bold;'}, ], overwrite=False]9: | 226 | 232 |
Ma trận nhầm lẫn cho nhiều mô hình dự đoán ung thư.
weather_df = pd.DataFrame[np.random.rand[10,2]*5, index=pd.date_range[start="2021-01-01", periods=10], columns=["Tokyo", "Beijing"]] def rain_condition[v]: if v