如何使用 pandas 更改数据框中文本的字体大小 [英] How to change the font-size of text in dataframe using pandas

查看:112
本文介绍了如何使用 pandas 更改数据框中文本的字体大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我研究了 Pandas 的样式文档,但无法准确地获得我的问题的特定和准确答案.我正在使用数据框读取 excel 文件并在我的程序中处理该数据框.最后,我使用 xlwings 库在另一个现有的 excel 文件中写入处理过的数据帧.

I have studied the styling documentation of pandas but not able to exactly get a particular and precise answer of my question. I am reading an excel file using dataframe and processing that dataframe in my program. At last I am writing processed dataframe in another existing excel file using xlwings library.

我正在使用-

import pandas as pd
import xlwings as xw
df = pd.read_excel("file.xlsx")

.
.  #Code for processing dataframe and writing dataframe in another excel file
.

在另一个现有的 excel 中写入此数据框之前,我想更改最终数据框中整个文本的字体大小.我无法找到方法.

Before writing this dataframe in another existing excel I want to change the font-size of whole text inside my final dataframe. I am not able to get the way to do it.

我在 Pandas 样式文档中找到了以下代码来实现它-

I have found following code in pandas styling document to achieve it-

def magnify():
    return [dict(selector="th",
                 props=[("font-size", "4pt")]),
            dict(selector="td",
                 props=[('padding', "0em 0em")]),
            dict(selector="th:hover",
                 props=[("font-size", "12pt")]),
            dict(selector="tr:hover td:hover",
                 props=[('max-width', '200px'),
                        ('font-size', '12pt')])
]

我在我的程序中使用了上面的代码,但我的数据框的字体大小保持不变.它对字体大小没有影响.我也尝试过其他一些使用样式的方法,但字体大小保持不变.

I have used above code in my program but font-size of my dataframe remains same.It creates no effect to font-size. I have tried some other methods using styling also but font-size remains same.

谁能以非常简单的方式告诉我如何使用 Pandas 或任何其他库仅更改最终数据帧的字体大小.因为我尝试了很多方法,但没有一种方法适合我.我只想更改字体大小,不想用我的字体做更多样式.

Can anyone please tell me in very simple manner how to only change the font-size of my final dataframe using pandas or any other library. Because I have tried many ways but none of ways works for me.I only want to change the font-size and not want to do more styling with my font.

推荐答案

您可以使用 set_properties().

df = pd.DataFrame({
    'date': ('2019-11-29', '2016-11-28'),
    'price': (0, 1),
})

df = df.style.set_properties(**{
    'background-color': 'grey',
    'font-size': '20pt',
})
df.to_excel('test.xlsx', engine='openpyxl')

您也可以使用 apply() 方法来自定义特定的单元格:

Also you can use apply() method to customize specific cells:

def custom_styles(val):
    # price column styles
    if val.name == 'price':
        styles = []
        # red prices with 0
        for i in val:
            styles.append('color: %s' % ('red' if i == 0 else 'black'))
        return styles
    # other columns will be yellow
    return ['background-color: yellow'] * len(val)


df = pd.DataFrame(...)
df = df.style.apply(custom_styles)
df.to_excel('test.xlsx', engine='openpyxl')

您也可以使用按元素工作的 applymap 方法.您可以在在文档中找到更多示例.

Also you can use applymap method which works elementwise. You can find more examples in docs.

希望这会有所帮助.

这篇关于如何使用 pandas 更改数据框中文本的字体大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆