Python Tabulate Dictionary containing two values per key
Question:
I am using the Tabulate 0.7.7 package to tabulate a dictionary with two values per key.
I have this code.
from tabulate import tabulate
d = {"Dave":("13", "Male") , "Sarah":("16", "Female")}
headers = ["Name", "Age", "Gender"]
print(tabulate(d.items(), headers = headers))
I want the following table to be produced –
Name Age Gender
------ ----- ---------
Dave 13 Male
Sarah 16 Female
However, the code is the following table –
Name Age
------ ----------------
Dave ('13', 'Male')
Sarah ('16', 'Female')
How can I solve this problem?
Answers:
If you flatten the dictionary items, from (k, (v1, v2)) to (k, v1, v2), you can get the correct format:
from tabulate import tabulate
d = {"Dave":("13", "Male") , "Sarah":("16", "Female")}
headers = ["Name", "Age", "Gender"]
print(tabulate([(k,) + v for k,v in d.items()], headers = headers))
Name Age Gender
------ ----- --------
Sarah 16 Female
Dave 13 Male
I’m not sure that it is possible given the structure of your input data d
.
Reformatting your data as such (see below) will give the desired result.
d = {'Name': ['Dave', 'Sarah'], 'Age': [13, 16], 'Gender': ['Male', 'Female']
I Know it’s very late but this solution also works.
from tabulate import tabulate
d = {"Dave":("13", "Male") , "Sarah":("16", "Female")}
headers = ["Name", "Age", "Gender"]
print(tabulate([(i,) +d[i] for i in d], headers = headers))
Hey I wanted to tabulate an array of dictionaries but couldn’t find it here so I did the logic my self and I’m posting here if someone needs it c:
print(tabulate([(x['id'], x['date']) for x in [i for i in post_ar]], headers=["Code", "Date"]))
I am using the Tabulate 0.7.7 package to tabulate a dictionary with two values per key.
I have this code.
from tabulate import tabulate
d = {"Dave":("13", "Male") , "Sarah":("16", "Female")}
headers = ["Name", "Age", "Gender"]
print(tabulate(d.items(), headers = headers))
I want the following table to be produced –
Name Age Gender
------ ----- ---------
Dave 13 Male
Sarah 16 Female
However, the code is the following table –
Name Age
------ ----------------
Dave ('13', 'Male')
Sarah ('16', 'Female')
How can I solve this problem?
If you flatten the dictionary items, from (k, (v1, v2)) to (k, v1, v2), you can get the correct format:
from tabulate import tabulate
d = {"Dave":("13", "Male") , "Sarah":("16", "Female")}
headers = ["Name", "Age", "Gender"]
print(tabulate([(k,) + v for k,v in d.items()], headers = headers))
Name Age Gender
------ ----- --------
Sarah 16 Female
Dave 13 Male
I’m not sure that it is possible given the structure of your input data d
.
Reformatting your data as such (see below) will give the desired result.
d = {'Name': ['Dave', 'Sarah'], 'Age': [13, 16], 'Gender': ['Male', 'Female']
I Know it’s very late but this solution also works.
from tabulate import tabulate
d = {"Dave":("13", "Male") , "Sarah":("16", "Female")}
headers = ["Name", "Age", "Gender"]
print(tabulate([(i,) +d[i] for i in d], headers = headers))
Hey I wanted to tabulate an array of dictionaries but couldn’t find it here so I did the logic my self and I’m posting here if someone needs it c:
print(tabulate([(x['id'], x['date']) for x in [i for i in post_ar]], headers=["Code", "Date"]))