你可以将一个数据框转换成一个垂直排列的Markdown表格。

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

How can I turn a dataframe into a markdown vertical table?

问题

编辑回答
我需要两种类型的表格一种是常规的水平表格视图为此我只是使用了to_markdown()并添加了代码以删除索引
对于垂直视图我复制并粘贴了下面的Ehsan的代码它完美地运行了

现在我想将这些粘贴到我的Excel表中以获得一个良好的视图这将需要大量的工作和特定的格式而且无法使用to_markdown()因为它会将DataFrame更改为字符串要将其作为索引获取您不能使用该函数您要么必须使用现有的DataFrame并找出如何添加新行和列要么创建一个具有正确数量的列和行的空DataFrame并使用必要的|格式进行填充之后您必须使用包含数据的DataFrame填充空/正确的单元格但是这太麻烦了所以我直接将字符串复制到正确的Excel表中使用以下代码

```python
#现在我们将保存此循环的迭代到正确的Excel表中
    file_source ='Source Used.xlsx'

    #加载Excel文件
    workbook = load_workbook(filename=file_source)

    #选择表“Sheet Used”
    ws4 = workbook["Project X"]

    #修改所需的单元格
    ws4.cell(row = 1, column = 1).value = newDataFrameWithStringFormat

    #保存文件
    workbook.save(filename=file_source)

从这里开始,每当我想要更新我的Markdown网站时,我只需从Excel表的第一个单元格中提取。它看起来不漂亮,但它有效。

编辑结束


<details>
<summary>英文:</summary>

EDIT Answer:
There are two types of tables I needed. One was the regular Horizontal table view. For that, I just used the &quot;to_markdown()&quot; with the added code to removed the indeces.
For the vertical view I copied and pasted Ehsan&#39;s code below and it worked perfectly.

Now, I wanted to paste these in a nice view to my excel sheet. That would have taken a bunch of work and formatting very specifically, and it won&#39;t work out with the to_markdown(), since that changes the DataFrame to a string. To get it as an index, you can&#39;t use that function. You&#39;d either have to take your existing DataFrame and figure out how to add new rows and columns, or: create an empty DataFrame with the proper amount of columns and rows, and fill it with the necessary &quot;|&quot; formatting. After that, you&#39;d have to fill out the empty/correct cells with data from your DataFrame that contains the data. However, this is too much work, so instead I just copied the string over directly into the proper excel sheet using this code:

#Now we are going to save this loop's iteration into the proper Excel sheet
file_source ='Source Used.xlsx'

#load excel file
workbook = load_workbook(filename=file_source)

#Pick the sheet &quot;Sheet Used&quot;
ws4 = workbook[&quot;Project X&quot;]

#modify the desired cell
ws4.cell(row = 1, column = 1).value = newDataFrameWithStringFormat

#save the file
workbook.save(filename=file_source)

From here, anytime I wanted to update my markdown website, I could just pull from the Excel sheet&#39;s first cell. It doesn&#39;t look pretty, but it works.
 
END EDIT
--------------------------------------------




I have a table that looks like this: 
df: 

| Project 1 | Project 2 | Project 3 |
|:---- |:------:| -----:|
| data 1  | data 2    | data 3 |
| data 4 | data 5 | data 6 |
|data 7 |data 8 | data 9 |


I want to edit this table so that it can work with, say, markdown, and be formatted well. However, to do that, I need to add a bunch of formatting to it (it basically needs to look the same as StackOverflow&#39;s table setup when writing this question). What function can I apply to it so that a new table is created that looks like this:

[![Table with headers on the rows][1]][1]


As you can see in this picture, the pipes and hyphens are each a new cell of data. Additionally, the Column HEADERS are at the beginning of the rows, since this is a horizontal format. How can I apply this formatting to dataframes of varying size?

This is what the new one should look like (I bolded the Project names myself): [![enter image description here][2]][2]

Now hypothetically, I can transpose the data and use to_markdown(), but that may still run into the issue of the top column being created as a column header. How can I avoid this by making a custom function to add in the pipes (&quot;|&quot;) and hyphens?


Thank you!


  [1]: https://i.stack.imgur.com/uTDVs.png
  [2]: https://i.stack.imgur.com/2jWiF.png

</details>


# 答案1
**得分**: 2

以下是代码的翻译部分:

```python
import pandas as pd

df=pd.DataFrame({"Project 1":["data 1", "data 4", "data 7"], "Project 2": ["data 2", "data 5", "data 8"], "Project 3": ["data 3", "data 6", "data 9"]})

df = df.transpose()
# 移除标题
df.columns = ["" for i in range(len(df.columns))]

# 使索引加粗 - ** ** 
df.index = ["**{}**".format(idx) for idx in df.index]

print(df.to_markdown())
#|               |        |        |        |
#|:--------------|:-------|:-------|:-------|
#| **Project 1** | data 1 | data 4 | data 7 |
#| **Project 2** | data 2 | data 5 | data 8 |
#| **Project 3** | data 3 | data 6 | data 9 |

如果您需要指定格式的Markdown表格的CSV版本,您可以修改字符串并将其保存为CSV文件:

markdown_text = df.to_markdown() # 来自上述代码
markdown_csv = markdown_text.replace("|", ",|,").replace(",\n,", "\n")
with open("results.csv", "w", encoding="utf-8") as fp:
    fp.write(markdown_csv)

结果将如下所示(如果您不需要**{}**,Markdown加粗,请注释掉代码的那部分):

你可以将一个数据框转换成一个垂直排列的Markdown表格。

英文:

A potential answer:

import pandas as pd

df=pd.DataFrame({&quot;Project 1&quot;:[&quot;data 1&quot;, &quot;data 4&quot;, &quot;data 7&quot;], &quot;Project 2&quot;: [&quot;data 2&quot;, &quot;data 5&quot;, &quot;data 8&quot;], &quot;Project 3&quot;: [&quot;data 3&quot;, &quot;data 6&quot;, &quot;data 9&quot;]})

df = df.transpose()
# remove header
df.columns = [&quot;&quot; for i in range(len(df.columns))]

# make the index bold - ** ** 
df.index = [&quot;**{}**&quot;.format(idx) for idx in df.index]
# **Project 1**  data 1  data 4  data 7
# **Project 2**  data 2  data 5  data 8
# **Project 3**  data 3  data 6  data 9

print(df.to_markdown())
#|               |        |        |        |
#|:--------------|:-------|:-------|:-------|
#| **Project 1** | data 1 | data 4 | data 7 |
#| **Project 2** | data 2 | data 5 | data 8 |
#| **Project 3** | data 3 | data 6 | data 9 |

Will be like this:

Project 1 data 1 data 4 data 7
Project 2 data 2 data 5 data 8
Project 3 data 3 data 6 data 9

Updated:

In case you want the csv version of the markdown table in your specified format, you can modify the string and save it as a csv file:

markdown_text = df.to_markdown() # from above code
markdown_csv= markdown_text.replace(&quot;|&quot;, &quot;,|,&quot;)[1:-1].replace(&quot;,\n,&quot;,&quot;\n&quot;)
with open(&quot;results.csv&quot;, &quot;w&quot;, encoding=&quot;utf-8&quot;) as fp:
    fp.write(markdown_csv)

The results will be (you can skip **{}**, markdown bold, in case you don't want it - just comment that part of the code):
你可以将一个数据框转换成一个垂直排列的Markdown表格。

huangapple
  • 本文由 发表于 2023年7月6日 21:53:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76629588.html
匿名

发表评论

匿名网友

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

确定