从pandas单元格中删除html格式 [英] Remove html formatting from pandas cell

查看:81
本文介绍了从pandas单元格中删除html格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在熊猫上有这个DataFrame:

I have this DataFrame on pandas:

import pandas as pd
df = pd.DataFrame({'CARGO': {53944: 'Driver',
57389: 'Driver',
  60851: 'Driver',
  64322: 'Driver',
  67771: 'Driver'},
 'DATE': {53944: '05/2015',
  57389: '06/2015',
  60851: '07/2015',
  64322: '08/2015',
  67771: '09/2015'},
 'DESCRICAO': {53944: '\\Salario R$ 788,00\nGratificacao Adicional R$ 251,00\nGRATIFICAÇÃO R$ 512,00\nINSS R$ -104,00',
  57389: '\\Salario R$ 788,00\nGratificacao Adicional R$ 251,00\nGRATIFICAÇÃO R$ 512,00\nINSS R$ -104,00',
  60851: '\\Salario R$ 788,00\n1/3 de Ferias R$ 516,95\nGratificacao Adicional R$ 251,00\nGRATIFICAÇÃO R$ 512,00\nINSS R$ -104,00',
  64322: '\\Salario R$ 788,00\nGratificacao Adicional R$ 251,00\nGRATIFICAÇÃO R$ 512,00\nINSS R$ -104,00',
  67771: '\\Salario R$ 788,00\nGratificacao Adicional R$ 225,90\nGRATIFICAÇÃO R$ 512,00\nINSS R$ -104,00'},
 'NOME': {53944: 'John Smith',
  57389: 'John Smith',
  60851: 'John Smith',
  64322: 'John Smith',
  67771: 'John Smith'}})

它呈现以下输出:

]

如何设置熊猫或Jupyter,以便: 1.显示纯文本 2.接受换行符('\ n')

How can I set up pandas or Jupyter so it will: 1. display plaintext 2. accept line break ('\n')

我希望它像这样:

推荐答案

您可以尝试以下方法,用html换行符<br>替换新的换行符,并显式使用.to_html()HTML进行显示,并将max_colwidth设置为-1,以便在转换为html时长行不会被截断:

You can try these things out, replace the new line character with html line break tag <br> and explicitly using .to_html() and HTML for the display, and also set the max_colwidth to be -1 so that the long line will not be truncated when converting to html:

from IPython.core.display import HTML
pd.set_option('display.max_colwidth', -1)
df['DESCRICAO'] = df['DESCRICAO'].str.replace('\$', '\\$').str.replace('\n', '<br>')
HTML(df.to_html(escape=False))

这篇关于从pandas单元格中删除html格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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