使用不带 Pandas 的 Python 将字典转换为具有列名的 table
Convert a dictionary to a table with column names using base Python without Pandas
我有一本看起来像这样的字典:
身高={'Andy':150,'Brenda':155,'Cindy':130}
我想要一个 table 一栏名字和一栏身高。我只想保留前 2 个高度。最终结果应如下所示:
有没有一种相对简单的方法可以在 Python 中得到这样的 table 而无需使用 Pandas?
您可以将词典转换为 CSV 文件,这将为您提供所需的输出。
import csv
heights = {'Andy':150, 'Brenda':155, 'Cindy':130}
last_key = "Cindy"
with open("filename.csv", "w") as csvfile:
writer = csv.writer(csvfile)
writer.writerow(["Name", "Height"])
for key, value in heights.items():
if key == last_key:
break
else:
writer.writerow([key, value])
您可以通过排序和切片轻松过滤字典以仅保存前两个值。
heights = {'Brenda': 155, 'Cindy': 130, 'Andy': 150}
# Sort Data and Keep Top 2 Results
top_two_heights = sorted(heights.items(), key=lambda v: -v[1])[:2]
print(top_two_heights)
输出:
[('Brenda', 155), ('Andy', 150)]
然后你可以任何你想要的数据。
如果您习惯使用外部包 tabulate 是一个不错的选择。
from tabulate import tabulate
# Header Labels
header_labels = ('Name', 'Height')
heights = {'Andy':150, 'Brenda':155, 'Cindy':130}
# Sort Data and Keep Top 2 Results
top_two_heights = sorted(heights.items(), key=lambda v: -v[1])[:2]
# Use Tabulate to Build Table
print(tabulate(top_two_heights, headers=header_labels, tablefmt='grid'))
输出:
+--------+----------+
| Name | Height |
+========+==========+
| Brenda | 155 |
+--------+----------+
| Andy | 150 |
+--------+----------+
如果你不想导入,你可以循环并打印出值:
# Header Labels
header_labels = ('Name', 'Height')
heights = {'Andy': 150, 'Brenda': 155, 'Cindy': 130}
# Sort Data and Keep Top 2 Results
top_two_heights = sorted(heights.items(), key=lambda v: -v[1])[:2]
fmt_str = '{:^10}|{:^10}'
print(fmt_str.format(*header_labels))
print('-' * 20)
for (a, b) in top_two_heights:
print(fmt_str.format(a, b))
输出:
Name | Height
--------------------
Brenda | 155
Andy | 150
我有一本看起来像这样的字典:
身高={'Andy':150,'Brenda':155,'Cindy':130}
我想要一个 table 一栏名字和一栏身高。我只想保留前 2 个高度。最终结果应如下所示:
有没有一种相对简单的方法可以在 Python 中得到这样的 table 而无需使用 Pandas?
您可以将词典转换为 CSV 文件,这将为您提供所需的输出。
import csv
heights = {'Andy':150, 'Brenda':155, 'Cindy':130}
last_key = "Cindy"
with open("filename.csv", "w") as csvfile:
writer = csv.writer(csvfile)
writer.writerow(["Name", "Height"])
for key, value in heights.items():
if key == last_key:
break
else:
writer.writerow([key, value])
您可以通过排序和切片轻松过滤字典以仅保存前两个值。
heights = {'Brenda': 155, 'Cindy': 130, 'Andy': 150}
# Sort Data and Keep Top 2 Results
top_two_heights = sorted(heights.items(), key=lambda v: -v[1])[:2]
print(top_two_heights)
输出:
[('Brenda', 155), ('Andy', 150)]
然后你可以任何你想要的数据。
如果您习惯使用外部包 tabulate 是一个不错的选择。
from tabulate import tabulate
# Header Labels
header_labels = ('Name', 'Height')
heights = {'Andy':150, 'Brenda':155, 'Cindy':130}
# Sort Data and Keep Top 2 Results
top_two_heights = sorted(heights.items(), key=lambda v: -v[1])[:2]
# Use Tabulate to Build Table
print(tabulate(top_two_heights, headers=header_labels, tablefmt='grid'))
输出:
+--------+----------+
| Name | Height |
+========+==========+
| Brenda | 155 |
+--------+----------+
| Andy | 150 |
+--------+----------+
如果你不想导入,你可以循环并打印出值:
# Header Labels
header_labels = ('Name', 'Height')
heights = {'Andy': 150, 'Brenda': 155, 'Cindy': 130}
# Sort Data and Keep Top 2 Results
top_two_heights = sorted(heights.items(), key=lambda v: -v[1])[:2]
fmt_str = '{:^10}|{:^10}'
print(fmt_str.format(*header_labels))
print('-' * 20)
for (a, b) in top_two_heights:
print(fmt_str.format(a, b))
输出:
Name | Height
--------------------
Brenda | 155
Andy | 150