Python ŌĆō 让Pandas DataFrame的列标题居中对齐
要将列标题居中对齐,可以使用 display.colheader_justify 和”center”值。导入所需库−
import pandas as pd
Python
创建具有2列的DataFrame −
dataFrame = pd.DataFrame(
{
"Car": ['BMW', 'Lexus', 'Tesla', 'Mustang', 'Mercedes', 'Jaguar'],
"Reg_Price": [7000.5057, 1500, 5000.9578, 8000, 9000.75768, 6000]
}
)
Python
现在,居中对齐列标题−
pd.set_option('display.colheader_justify', 'center')
Python
示例
以下是代码−
import pandas as pd
# Create DataFrame
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
Python
输出
将产生以下输出−
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
Python