尝试使用`df.style.set_properties`更改数据框的列宽,但不起作用。

huangapple go评论82阅读模式
英文:

Trying to change column width of dataframe with df.style.set_properties, but does not work

问题

我正在尝试通过使用pandas的样式和set_properties来更改我的数据框的列宽,但是宽度参数似乎是唯一不起作用的参数,而其他参数起作用并应用了适当的更改。

with pd.ExcelWriter(results_path, mode="a", engine="openpyxl", if_sheet_exists="overlay") as writer:
     df.style.set_properties(
     **{'width': '600px', 'text-align': 'right', 'background-color': '#B4C6E7', 'color': 'black',     
     'border': '1.3px solid black'}).to_excel \
     (writer, sheet_name="Sheet", header=True, startrow=0, startcol=0,index=False)

我还尝试使用pd.set_option('display.max_colwidth', None),但那也没有效果。

英文:

I am trying to change the column width of my dataframe through the use of pandas' style and set_properties, but the width parameter is the only thing that doesn't seem to work while the rest of the parameters work and apply the appropriate changes.

with pd.ExcelWriter(results_path, mode="a", engine="openpyxl", if_sheet_exists="overlay") as writer:
     df.style.set_properties(
     **{'width': '600px', 'text-align': 'right', 'background-color': '#B4C6E7', 'color': 'black',     
     'border': '1.3px solid black'}).to_excel \
     (writer, sheet_name="Sheet", header=True, startrow=0, startcol=0,index=False)

I also tried using pd.set_option('display.max_colwidth', None), but that did not have an effect either.

答案1

得分: 1

set_properties() 中的 width 参数用于设置列的内容宽度,而不是整个列本身的宽度。此外,set_properties() 更专注于单元格级别的样式和格式设置,而 set_column() 用于控制列的宽度。此外,重要的是要知道 pd.set_option('display.max_colwidth', None) 影响在控制台打印时文本的显示方式,而不影响输出中的实际列宽度。要更改整个列的宽度,您需要使用下面示例中显示的其他方法:

import pandas as pd

# 您的 DataFrame
data = {'Column1': ['aaa', 'bbb', 'ccc'],
        'Column2': ['111', '222', '333']}
df = pd.DataFrame(data)

# 使用 ExcelWriter 更改列宽度
with pd.ExcelWriter('output.xlsx', engine='openpyxl') as writer:
    df.to_excel(writer, sheet_name='Sheet1', startrow=1, header=False)

    # 获取 xlsxwriter 工作簿和工作表对象
    workbook = writer.book
    worksheet = writer.sheets['Sheet1']

    # 设置特定列的列宽和格式
    worksheet.set_column('B:B', 20)  # 例如,设置列 B 的宽度

    # 使用 pandas 样式添加样式
    df.style.applymap(lambda x: 'background-color: #B4C6E7; color: black; border: 1.3px solid black')

    writer.save()
英文:

The width parameter in set_properties() is used to set the width of a column's content, not the entire column itself. Also set_properties() is more focused on cell-leveling styling and formatting, while set_column() is used to control the column width. Along with that it's important to know that pd.set_option('display.max_colwidth',None) affects how text is displayed when printed to the console, not the actual column width in the output. To change the width of the entire column, you would need to use other methods shown below:

import pandas as pd

# Your DataFrame
data = {'Column1': ['aaa', 'bbb', 'ccc'],
        'Column2': ['111', '222', '333']}
df = pd.DataFrame(data)

# Change the column width using ExcelWriter
with pd.ExcelWriter('output.xlsx', engine='openpyxl') as writer:
    df.to_excel(writer, sheet_name='Sheet1', startrow=1, header=False)

    # Get the xlsxwriter workbook and worksheet objects
    workbook  = writer.book
    worksheet = writer.sheets['Sheet1']

    # Set the column width and format for your specific column
    worksheet.set_column('B:B', 20)  # For example, sets the width of column B

    # Add styling using pandas' style
    df.style.applymap(lambda x: 'background-color: #B4C6E7; color: black; border: 1.3px solid black')

    writer.save()

huangapple
  • 本文由 发表于 2023年8月4日 06:08:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76831860.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定