Python中的Pandas.reset_option()函数
Pandas有一个选项系统,可以让我们定制其行为的某些方面,与显示有关的选项是用户最可能调整的。让我们看看如何将一个指定的选项的值重设为其默认值。
reset_option()
语法 :
pandas.reset_option(pat)
参数 :
- pat :应该匹配单个选项的Regexp。
例子1 : 我们将使用pandas.get_option()改变一个选项的值,然后我们将使用pandas.reset_option()将其重置为默认值。
# importing the module
import pandas as pd
# setting the values
pd.set_option("display.max_rows", 10)
pd.set_option("display.min_rows", 2)
pd.set_option("display.max_columns", 5)
pd.set_option("display.html.border", 3)
pd.set_option("io.excel.xlsm.reader", "openpyxl")
# displaying the values
print("The altered values are : ")
print("Value of max_rows : " +
str(pd.get_option("display.max_rows")))
print("Value of min_rows : " +
str(pd.get_option("display.max_columns")))
print("Value of max_columns : " +
str(pd.get_option("display.max_columns")))
print("Value of border : " +
str(pd.get_option("display.html.border")))
print("Value of xlsm reader : " +
str(pd.get_option("io.excel.xlsm.reader")))
# resetting the values to default
pd.reset_option("display.max_rows")
pd.reset_option("display.min_rows")
pd.reset_option("display.max_columns")
pd.reset_option("display.html.border")
pd.reset_option("io.excel.xlsm.reader")
# displaying the default values
print("\nThe default values are : ")
print("Value of max_rows : " +
str(pd.get_option("display.max_rows")))
print("Value of min_rows : " +
str(pd.get_option("display.max_columns")))
print("Value of max_columns : " +
str(pd.get_option("display.max_columns")))
print("Value of border : " +
str(pd.get_option("display.html.border")))
print("Value of xlsm reader : " +
str(pd.get_option("io.excel.xlsm.reader")))
输出 :
例子2 :我们可以通过在pandas.reset_option()函数中传递 “all “作为参数来一次性重置所有选项的值,而不是单独重置不同选项的值。
# importing the module
import pandas as pd
# setting the values
pd.set_option("display.max_rows", 10)
pd.set_option("display.min_rows", 2)
pd.set_option("display.max_columns", 5)
pd.set_option("display.html.border", 3)
pd.set_option("io.excel.xlsm.reader", "openpyxl")
# displaying the values
print("The altered values are : ")
print("Value of max_rows : " +
str(pd.get_option("display.max_rows")))
print("Value of min_rows : " +
str(pd.get_option("display.max_columns")))
print("Value of max_columns : " +
str(pd.get_option("display.max_columns")))
print("Value of border : " +
str(pd.get_option("display.html.border")))
print("Value of xlsm reader : " +
str(pd.get_option("io.excel.xlsm.reader")))
# resetting the values to default
pd.reset_option("all")
# displaying the default values
print("\nThe default values are : ")
print("Value of max_rows : " +
str(pd.get_option("display.max_rows")))
print("Value of min_rows : " +
str(pd.get_option("display.max_columns")))
print("Value of max_columns : " +
str(pd.get_option("display.max_columns")))
print("Value of border : " +
str(pd.get_option("display.html.border")))
print("Value of xlsm reader : " +
str(pd.get_option("io.excel.xlsm.reader")))
输出 :