英文:
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 "to_markdown()" with the added code to removed the indeces.
For the vertical view I copied and pasted Ehsan'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't work out with the to_markdown(), since that changes the DataFrame to a string. To get it as an index, you can't use that function. You'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 "|" formatting. After that, you'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 "Sheet Used"
ws4 = workbook["Project X"]
#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's first cell. It doesn'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'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 ("|") 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加粗,请注释掉代码的那部分):
英文:
A potential answer:
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()
# remove header
df.columns = ["" for i in range(len(df.columns))]
# make the index bold - ** **
df.index = ["**{}**".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("|", ",|,")[1:-1].replace(",\n,","\n")
with open("results.csv", "w", encoding="utf-8") 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):
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论