Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/300.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何仅更改第一个“字体”;列名";熊猫_Python_Pandas - Fatal编程技术网

Python 如何仅更改第一个“字体”;列名";熊猫

Python 如何仅更改第一个“字体”;列名";熊猫,python,pandas,Python,Pandas,我有一个熊猫数据框,有3列。我只需要更改第一个“列名”的字体,并使第二和第三个列名使用不同的字体。我正在使用render函数生成CSS标记 我尝试使用df.style.set_table_styles函数,但它对所有列名(列标题)应用相同的字体 是否有办法将style函数的应用限制为仅第一列标题 df.style.set\u table\u样式 df.style.set_属性(子集=df.columns[0],**{'text-align':'left','font-family':'Couri

我有一个熊猫数据框,有3列。我只需要更改第一个“列名”的字体,并使第二和第三个列名使用不同的字体。我正在使用render函数生成CSS标记

我尝试使用df.style.set_table_styles函数,但它对所有列名(列标题)应用相同的字体

是否有办法将style函数的应用限制为仅第一列标题

df.style.set\u table\u样式
df.style.set_属性(子集=df.columns[0],**{'text-align':'left','font-family':'Courier','font-size':'10px'})
df=pd.DataFrame({'A':[1,2,3],
“B”:[4,5,6],
‘C’:[7,8,9],
“D”:[1,3,5],
“E”:[5,3,6],
‘F’:[7,4,3]})
df=df.style.set_属性(子集=df.columns[0],**{'text-align':'left','font-family':'Cambria','font-size':'10px'})
打印(df.render())

我只希望“A”有Cambria字体,其余(B、C、D等)有不同的字体。

您必须使用一些CSS技巧来选择第一列标题:

def set_header_font():
    return [dict(selector="th:nth-child(2)",
                 props=[("font-size", "14pt"),("font-family", 'Cambria')])]

df.style.set_table_styles(set_header_font())
n子元素(2)
选择表中的第二个
th
元素——第二个
th
,因为第一个
th
用于数据帧索引名(在您的示例中是空字符串)