首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在使用pandas.to_markdown时删除对齐标记表

如何在使用pandas.to_markdown时删除对齐标记表
EN

Stack Overflow用户
提问于 2022-08-10 00:57:15
回答 1查看 79关注 0票数 0

谢谢你的考虑。

总结问题

当我从excel转到减价时,我使用熊猫库。

将excel中的clipboard.

  • Make表复制到pd.read_clipboard

  • Make str( pd.to_markdown

标记表)中的数据

来自pd.to_markdown的Str也应用了对齐。

但是,当我通过pd.to_markdown生成str时,我想删除对齐表中的标记。

以下是一些例子。

剪贴板上的文本

代码语言:javascript
运行
复制
post_it width   height
653 51  38
654 76  76
656 76  51
657 102 76
代码语言:javascript
运行
复制
import pandas as pd
df=pd.read_clipboard()
markdown_table=df.to_markdown(index=False)
代码语言:javascript
运行
复制
print(df)
代码语言:javascript
运行
复制
   post_it  width  height
0      653     51      38
1      654     76      76
2      656     76      51
3      657    102      76
代码语言:javascript
运行
复制
print(markdown_table)

我从pd.to_markdown那里得到的

代码语言:javascript
运行
复制
|   post_it |   width |   height |
|----------:|--------:|---------:|
|       653 |      51 |       38 |
|       654 |      76 |       76 |
|       656 |      76 |       51 |
|       657 |     102 |       76 |

我想要pd.to_markdown的数据

没有冒号2行

代码语言:javascript
运行
复制
|   post_it |   width |   height |
|-----------|---------|----------|
|       653 |      51 |       38 |
|       654 |      76 |       76 |
|       656 |      76 |       51 |
|       657 |     102 |       76 |
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-08-10 01:20:59

to_markdown()方法有一个tablefmt参数。

这看起来像你想要的:

代码语言:javascript
运行
复制
print(df.to_markdown(tablefmt="github", index=False))

# Output

|   post_it |   width |   height |
|-----------|---------|----------|
|       653 |      51 |       38 |
|       654 |      76 |       76 |
|       656 |      76 |       51 |
|       657 |     102 |       76 |

如果这不是您想要的,有许多格式可供选择。它们列在文档中:https://github.com/astanin/python-tabulate

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73299563

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档