如何使用 pandas 更改数据框中文本的字体大小
How to change the font-size of text in dataframe using pandas
我已经研究了 pandas 的样式文档,但无法准确地得到我的问题的特定和准确的答案。我正在使用数据帧读取 excel 文件并在我的程序中处理该数据帧。最后,我正在使用 xlwings 库在另一个现有 excel 文件中编写处理过的数据框。
我正在使用-
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 之前,我想更改最终数据框中整个文本的字体大小。我找不到方法。
我在 pandas 样式文档中找到了以下代码来实现它-
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')])
]
我在我的程序中使用了上面的代码,但我的数据框的字体大小保持不变 same.It 对字体大小没有影响。我也尝试了一些其他使用样式的方法,但字体大小保持不变。
谁能以非常简单的方式告诉我如何使用 pandas 或任何其他库仅更改最终数据框的字体大小。因为我尝试了很多方法,但是 none 的方法适用于 me.I 只想更改字体大小而不想用我的字体做更多样式。
您可以使用 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()
方法自定义特定的单元格:
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
方法,该方法按元素工作。您可以找到更多示例 in docs.
希望对您有所帮助。
我已经研究了 pandas 的样式文档,但无法准确地得到我的问题的特定和准确的答案。我正在使用数据帧读取 excel 文件并在我的程序中处理该数据帧。最后,我正在使用 xlwings 库在另一个现有 excel 文件中编写处理过的数据框。
我正在使用-
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 之前,我想更改最终数据框中整个文本的字体大小。我找不到方法。
我在 pandas 样式文档中找到了以下代码来实现它-
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')])
]
我在我的程序中使用了上面的代码,但我的数据框的字体大小保持不变 same.It 对字体大小没有影响。我也尝试了一些其他使用样式的方法,但字体大小保持不变。
谁能以非常简单的方式告诉我如何使用 pandas 或任何其他库仅更改最终数据框的字体大小。因为我尝试了很多方法,但是 none 的方法适用于 me.I 只想更改字体大小而不想用我的字体做更多样式。
您可以使用 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()
方法自定义特定的单元格:
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
方法,该方法按元素工作。您可以找到更多示例 in docs.
希望对您有所帮助。