Bài 16: Options và Customize – Python Panda

Trang chủ » Training » Bài 16: Options và Customize – Python Panda
22/02/2022 Training 91 viewed
Pandas cung cấp API để tùy chỉnh một số tính chất của nó, API gồm 5 hàm như sau :
  • get_option()
  • set_option()
  • reset_option()
  • describe_option()
  • option_context()

1. get_option(param)

get_option nhận một tham số duy nhất và trả về giá trị như đã cho trong đầu ra bên dưới
display.max_rows
Hiển thị số giá trị mặc định. Trình thông dịch đọc giá trị này và hiển thị các hàng có giá trị này làm giới hạn trên để hiển thị.
import pandas as pd
print pd.get_option("display.max_rows")
Kết quả :
60
display.max_columns
import pandas as pd
print pd.get_option("display.max_columns")
Kết quả :
20
Ở đây, 60 và 20 là giá trị tham số cấu hình mặc định.

2. set_option(param,value)

set_option nhận hai đối số và đặt giá trị cho tham số như sau :
display.max_rows :
Sử dụng set_option (), ta có thể thay đổi số lượng hàng mặc định được hiển thị.
import pandas as pd

pd.set_option("display.max_rows",80)

print pd.get_option("display.max_rows")
Kết quả :
80
display.max_columns :
Sử dụng set_option (), ta có thể thay đổi số lượng hàng mặc định được hiển thị.
import pandas as pd

pd.set_option("display.max_columns",30)

print pd.get_option("display.max_columns")
Kết quả :
30

3. reset_option(param) :

reset_option nhận một đối số và đặt giá trị trở lại giá trị mặc định.
display.max_rows ;
Sử dụng reset_option (), chúng ta có thể thay đổi giá trị trở lại số hàng mặc định được hiển thị.
import pandas as pd

pd.reset_option("display.max_rows")
print pd.get_option("display.max_rows")
Kết quả :
60

4. describe_option(param) :

description_option in ra mô tả của đối số.
display.max_rows
Sử dụng reset_option (), ta có thể thay đổi giá trị trở lại số hàng mặc định được hiển thị.
import pandas as pd
pd.describe_option("display.max_rows")
Kết quả :
display.max_rows : int
   If max_rows is exceeded, switch to truncate view. Depending on
   'large_repr', objects are either centrally truncated or printed as
   a summary view. 'None' value means unlimited.

   In case python/IPython is running in a terminal and `large_repr`
   equals 'truncate' this can be set to 0 and pandas will auto-detect
   the height of the terminal and print a truncated object which fits
   the screen height. The IPython notebook, IPython qtconsole, or
   IDLE do not run in a terminal and hence it is not possible to do
   correct auto-detection.
   [default: 60] [currently: 60]

5. option_context()

display.max_rows :
Sử dụng option_context (), ta có thể đặt giá trị tạm thời.
import pandas as pd
with pd.option_context("display.max_rows",10):
   print(pd.get_option("display.max_rows"))
   print(pd.get_option("display.max_rows"))
Kết quả :
10
10
sự khác biệt giữa câu lệnh in đầu tiên và thứ hai. Câu lệnh đầu tiên in giá trị được đặt bởi option_context () là giá trị tạm thờii. Sau ngữ cảnh with, câu lệnh in thứ hai in ra giá trị được cấu hình.
  1. display.max_rows : Hiển thị số hàng tối đa để hiển thị
  2. 2 display.max_columns : Hiển thị số cột tối đa để hiển thị
  3. display.expand_frame_repr : Hiển thị DataFrames để kéo dài trang
  4. display.max_colwidth : Hiển thị chiều rộng cột tối đa
  5. display.precision : Hiển thị độ chính xác cho số thập phân
Chia sẻ:
Tags:
TOP HOME