我使用以下命令:
table(factor("list",levels=1:"n")
Run Code Online (Sandbox Code Playgroud)
使用 "list": (example) a = c(1,3,4,4,3)
and levels = 1:5,还要考虑 2 和 5。对于非常大的数据集,我的代码似乎非常无效。
有没有人知道隐藏的库或代码片段以使其更快?
有没有办法在 python 中指定带有合并单元格的多个标题?
示例数据集:
from tabulate import tabulate
cols = ["ID", "Config\nA", "Config\nB", "Config\nC", "Config\nD", "Oth"]
rows = [[ "0x0", "2", "0", "0", "4", "3"],
[ "0x1", "0", "0", "0", "0", "4"],
[ "0x2", "0", "2", "0", "1", "5"]]
print(tabulate(rows, headers=cols,tablefmt="pretty"))
Run Code Online (Sandbox Code Playgroud)
表格的当前输出:
+-----+--------+--------+--------+--------+--------+
| ID | Config | Config | Config | Config | Oth |
| | A | B | C | D | |
+-----+--------+--------+--------+--------+--------+
| 0x0 | 2 | 0 | 0 | 4 | 3 …Run Code Online (Sandbox Code Playgroud) 我正在使用制表(https://pypi.org/project/tabulate/),我想用千位分隔符格式化数字列并将其右对齐。这就是我所做的,它没有正确对齐我的专栏。
import pandas as pd
rom tabulate import tabulate
df = pd.DataFrame([{'size':225612466, 'path': '/etc'}, {'size':66, 'path': '/home'}])
df['size'] = df['size'].apply(lambda x: "{:,}".format(x).rjust(15))
print(tabulate(df, headers='keys', tablefmt='psql', showindex=False))
+--------+-------------+
| path | size |
|--------+-------------|
| /etc | 225,612,466 |
| /home | 66 |
+--------+-------------+
Run Code Online (Sandbox Code Playgroud)
我希望它是这样的:
+--------+-----------------+
| path | size |
|--------+-----------------|
| /etc | 225,612,466 |
| /home | 66 |
+--------+-----------------+
Run Code Online (Sandbox Code Playgroud)
谢谢,
我想知道是否有一些参数可以传递给制表模块并指定列之间的空格数量。
我可以看到默认有 2 个空格,但我没有找到如何操作它。这是脚本示例。
from tabulate import tabulate
data = [
["Name","LastName", "Age"],
["Juan","Lopez", "22"],
["Luisa","Perez", "24"],
["Ana","Sanchez", "23"]
]
print (tabulate(data, stralign="right", tablefmt="plain"))
Run Code Online (Sandbox Code Playgroud)
输出:
Name LastName Age
Juan Lopez 22
Luisa Perez 24
Ana Sanchez 23
Run Code Online (Sandbox Code Playgroud)
完整的任务是从计划文本中提取数据并对其进行组织。我解决问题的一种方法是在每个数据列之间添加空列,但这可能不是最有效的方法。
我正在使用 tabulate 0.75 生成 LaTeX 表。生成表格的函数 tabulate 有一个内置函数来格式化输入。但我想定义每列本身的小数位数。例如
a b
18.12 16
17.47 15
Run Code Online (Sandbox Code Playgroud)
这是内置函数 (floatfmt=".2f"),它运行完美 - 但它对每一列的作用都是相同的。
tabulate(tables[i], headers[i], numalign="center", tablefmt="latex_booktabs", floatfmt=".2f")
Run Code Online (Sandbox Code Playgroud)
我通过以下方式获取我的数据
tables = [(list(zip(
unumpy.nominal_values(dt),
unumpy.nominal_values(U),
unumpy.nominal_values(I),
unumpy.nominal_values(E),
unumpy.std_devs(E)))), ............and so on
Run Code Online (Sandbox Code Playgroud)
我在列表中使用 ufloat
array123 = ufloat(nominalvalues, errors)
Run Code Online (Sandbox Code Playgroud)
经常这样做是因为它使处理错误变得非常容易。有谁知道一种简单的格式化方法吗?
"{:.2f}".format(unumpy.nominal_values(E))
TypeError: non-empty format string passed to object.__format__
Run Code Online (Sandbox Code Playgroud)
这不行。
提前致谢。
我正在使用该tabulate模块来打印固定宽度的文件,并且有一列需要进行格式化,小数点左边有 19 位,小数点右边有 2 位。
import pandas as pd
from tabulate import tabulate
df = pd.DataFrame.from_dict({'A':['x','y','z'],
'B':[1,1.1,11.21],'C':[34.2334,81.1,11]})
df
Out[4]:
A B C
0 x 1.00 34.2334
1 y 1.10 81.1000
2 z 11.21 11.0000
df['C'] = df['C'].apply(lambda x: format(x,'0>22.2f'))
df
Out[6]:
A B C
0 x 1.00 0000000000000000034.23
1 y 1.10 0000000000000000081.10
2 z 11.21 0000000000000000011.00
print(tabulate(df))
- - ----- -----
0 x 1 34.23
1 y 1.1 81.1
2 z 11.21 11
- - ----- -----
Run Code Online (Sandbox Code Playgroud)
有什么方法可以保留 …
我正在尝试以表格格式打印 python 字典,但在使用字典时遇到困难。以下是我的字典:
\n\nboard_dict =\n\n {\n \'Done\': {\n \'point\': 0.0,\n \'items\': 1\n },\n \'Doing\': {\n \'point\': 24.0,\n \'items\': 3\n },\n \'New\': {\n \'point\': 0.0,\n \'items\': 2\n },\n \'Stuck\': {\n \'point\': 19.0,\n \'items\': 3\n },\n \'Ready to Test\': {\n \'point\': Decimal(\'1\'),\n \'items\': 1\n }\n }\nRun Code Online (Sandbox Code Playgroud)\n\n我正在尝试设计这样的东西:
\n\nColumn Items Points\n------ ----- ------\nSprint Backlog 0 0\nDoing 3 24\nStuck 3 19\nCode Review 0 0\nReady to Test 1 1\nDone 1 0\n------ \nTotal 8 44\n------ \nRun Code Online (Sandbox Code Playgroud)\n\n我已经使用 python https://pypi.org/project/tabulate/尝试了以下操作tabulate
我正在尝试发送带有一些 CSS 配置的表格作为正文的电子邮件。为此,我有以下代码:
import csv
from tabulate import tabulate
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import smtplib
text = """Hello, Friend.Here is your data:{table}Regards,Me"""
html = """"\
<html>
<head>
<style type="text/css">
.tg {border-collapse:collapse;border-spacing:0;}
.tg td{font-family:Arial, sans-serif;font-size:14px;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;border-color:black;}
.tg th{font-family:Arial, sans-serif;font-size:14px;font-weight:normal;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;border-color:black;}
.tg .tg-0lax{text-align:left;vertical-align:top}
</style>
</head>
<body>
<table class="tg">
<tr>
<th class="tg-0lax">Environment</th>
<th class="tg-0lax">Date</th>
<th class="tg-0lax">Error_Type</th>
<th class="tg-0lax">Error_Object</th>
<th class="tg-0lax">Description</th>
</tr>
<tr>
<td class="tg-0lax">DEV</td>
<td class="tg-0lax">15/03/2019</td>
<td class="tg-0lax">ERROR</td>
<td class="tg-0lax">Table</td>
<td class="tg-0lax">More columns than expected</td>
</tr>
</table>
</body>
</html>""" …Run Code Online (Sandbox Code Playgroud) 我将 pandas 数据帧输出到我正在编写的报告中,这很容易,就像您现在可以将df.to_markdown()数据帧转换为 Markdown 表一样,然后 pandoc 可以生成报告。
我们可以通过floatfmt=".3g"作为参数传递来非常精细地控制特定的格式细节,其操作在这里解释得非常清楚:
...精确的规则如下:假设用表示类型“e”和精度 p-1 格式化的结果将具有指数 exp。然后,如果 m <= exp < p,其中 m 为 -4(对于浮点数)和 -6(对于小数),则数字将采用表示类型“f”和精度 p-1-exp 进行格式化。否则,数字将采用表示类型“e”和精度 p-1 进行格式化。在这两种情况下,都会从有效数字中删除无意义的尾随零,并且如果小数点后面没有剩余数字,也会删除小数点,除非使用“#”选项...
除了我不希望删除尾随零,而且我看不到一种简单的方法来防止它们被删除。
如果您想要一个可重现的示例,请看这里:
import pandas as pd
df = pd.DataFrame({'Numbers':[0.100, 0.123, 0.101, 0.099, 0.120, 0.012]})
print(df.to_markdown(showindex=False, floatfmt=".2g"))
Run Code Online (Sandbox Code Playgroud)
给出
| Numbers |
|----------:|
| 0.1 |
| 0.12 |
| 0.1 |
| 0.099 |
| 0.12 |
| 0.012 |
Run Code Online (Sandbox Code Playgroud)
我可以将其更改为指定十进制数字,就像这样
print(df.to_markdown(showindex=False, floatfmt=".2f"))
Run Code Online (Sandbox Code Playgroud)
这使
| Numbers |
|----------:| …Run Code Online (Sandbox Code Playgroud) 我想导出一个由PROC TABULATE. 我的代码是这样的:
ODS EXCEL FILE="myFile.xlsx" (options sheet_name="CRIME TYPE");
PROC TABULATE DATA=myData;
TITLE 'myTitle';
BY crime_type;
CLASS year;
CLASS nation / ORDER=FREQ;
TABLES year, nationality / CONDENSE;
RUN;
ODS EXCEL CLOSE;
Run Code Online (Sandbox Code Playgroud)
这为我创建了一个包含不同工作表的 excel 文件:
THEFT
country1 country2 country3 ...
--------------------------------------
1990
1991
1992
--------------------------------------
ASSAULT
country1 country2 country3 ...
--------------------------------------
1990
1991
1992
--------------------------------------
Run Code Online (Sandbox Code Playgroud)
不幸的是,这些表没有不同罪行(盗窃、袭击等)的名称,而是被称为“犯罪类型 1”、“犯罪类型 2”等等(SHEET_NAME="CRIME TYPE")。
有谁知道如何根据变量的值命名工作表crime_type?