Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/278.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 pandas DataFrame:标题中的下一行_Python_Pandas_Dataframe - Fatal编程技术网

Python pandas DataFrame:标题中的下一行

Python pandas DataFrame:标题中的下一行,python,pandas,dataframe,Python,Pandas,Dataframe,编辑:原来的Q是关于将颜色更改为列的。。这是另一个问题 我正在尝试基本上\n列标题,如下图所示 到目前为止,我得到了这个: df.style.set_table_styles( [{'selector':'th', 'props': [('background','#36304a'), ('color','white'), ('text-align','c

编辑:原来的Q是关于将颜色更改为列的。。这是另一个问题

我正在尝试基本上
\n
列标题,如下图所示

到目前为止,我得到了这个:

df.style.set_table_styles(
            [{'selector':'th',
             'props': [('background','#36304a'),
                       ('color','white'),
                       ('text-align','center')]},

             {'selector':'tr-nth-of-type(odd)',
             'props': [('background', '#d9d9d9')]},

             {'selector':'tr-nth-of-type(even)',
             'props': [('background', 'white')]},
             ])\
    .set_properties(subset=["Instument \n Name", "Etoro \n Symbol"], **{'text-align': 'left'})\
    .set_properties(subset=["#", "% Volume Above 20 Day \n Average","Yesterday's Percent \n Change"], **{'text-align': 'center'})\
    .format({'% Volume Above 20 Day \n Average': lambda x: '%.2f'%(x*100) +'%',
            "Yesterday's Percent \n Change": lambda x: '%.2f'%(x*100) +'%' })\
    .hide_index()
但它看起来是这样的(没有下线):

我希望它看起来像那样(很抱歉质量太差):


谢谢

您可以使用
style.applymap(func)
进行设置。见官员

def颜色_绿色(x):
如果x<0.0:
颜色=‘红色’
其他:
颜色=‘绿色’
返回“颜色:%s”%color
applymap(颜色为绿色,子集=pd.indexlice[:,[‘昨天的百分比变化’]))

太棒了!很好用!非常感谢。有问题吗?我不知道如何在标题中“\n”下。。有什么想法吗?
df.style.set_table_styles([dict(selector=“th”,props=[('max-width','50px'))])))
这个设置有用吗?很高兴听到这个。它值得承认吗?英雄联盟
def color_green(x):
    if x < 0.0:
        color = 'red'
    else:
        color = 'green'
    return 'color: %s' % color
df.style.applymap(color_green, subset=pd.IndexSlice[:,['Yesterday's Percent Change']])