Python - 居中对齐 Pandas DataFrame 的列标题
要居中对齐列标题,请使用display.colheader_justify和'center'值。导入需要的库-
import pandas as pd
创建一个具有2列的DataFrame-
dataFrame = pd.DataFrame( { "Car": ['BMW', 'Lexus', 'Tesla', 'Mustang', 'Mercedes', 'Jaguar'], "Reg_Price": [7000.5057, 1500, 5000.9578, 8000, 9000.75768, 6000] } )
现在,将列标题居中对齐-
pd.set_option('display.colheader_justify', 'center')
示例
以下是代码-
import pandas as pd #创建数据帧 dataFrame = pd.DataFrame( { "Car": ['BMW', 'Lexus', 'Tesla', 'Mustang', 'Mercedes', 'Jaguar'], "Reg_Price": [7000.5057, 1500, 5000.9578, 8000, 9000.75768, 6000] } ) print"DataFrame ...\n",dataFrame pd.set_option('display.colheader_justify', 'center') print"\nUpdated dataframe with center aligned column headers...\n", dataFrame输出结果
这将产生以下输出-
DataFrame ... Car Reg_Price 0 BMW 7000.50570 1 Lexus 1500.00000 2 Tesla 5000.95780 3 Mustang 8000.00000 4 Mercedes 9000.75768 5 Jaguar 6000.00000 Updated dataframe with center aligned column headers... Car Reg_Price 0 BMW 7000.50570 1 Lexus 1500.00000 2 Tesla 5000.95780 3 Mustang 8000.00000 4 Mercedes 9000.75768 5 Jaguar 6000.00000