Sea*_*yen 8 python-3.x tabulate
我正在使用制表(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)
谢谢,
我猜缺少的参数tabulate是numalign="right",但你必须摆脱手动rjust将值更改为str:
import pandas as pd
rom tabulate import tabulate
df = pd.DataFrame([{'size':225612466, 'path': '/etc'}, {'size':66, 'path': '/home'}])
print(tabulate(df, headers='keys', tablefmt='psql', showindex=False, numalign="right"))
Run Code Online (Sandbox Code Playgroud)
给出输出:
+--------+-----------+
| path | size |
|--------+-----------|
| /etc | 225612466 |
| /home | 66 |
+--------+-----------+
Run Code Online (Sandbox Code Playgroud)
如果您想保留千位分隔符,则必须使用以下命令保留格式并将字符串右对齐stralign:
print(tabulate(df, headers='keys', tablefmt='psql', showindex=False, stralign="right"))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12648 次 |
| 最近记录: |