4

I'm trying to print a python dict in tabular format but facing difficulty in working with dict. Following is my dict:

board_dict =

    {
        'Done': {
            'point': 0.0,
            'items': 1
        },
        'Doing': {
            'point': 24.0,
            'items': 3
        },
        'New': {
            'point': 0.0,
            'items': 2
        },
        'Stuck': {
            'point': 19.0,
            'items': 3
        },
        'Ready to Test': {
            'point': Decimal('1'),
            'items': 1
        }
    }

I'm trying to design something like this:

Column         Items Points
------         ----- ------
Sprint Backlog 0     0
Doing          3     24
Stuck          3     19
Code Review    0     0
Ready to Test  1     1
Done           1     0
------               
Total          8     44
------  

I have tried the following using python tabulate https://pypi.org/project/tabulate/

print(tabulate(board_dict, headers="keys", tablefmt="fancy_grid"))


╒════════╤═════════╤═══════╤═════════╤═════════════════╕
│ Done   │ Doing   │ New   │ Stuck   │ Ready to Test   │
╞════════╪═════════╪═══════╪═════════╪═════════════════╡
│ point  │ point   │ point │ point   │ point           │
├────────┼─────────┼───────┼─────────┼─────────────────┤
│ items  │ items   │ items │ items   │ items           │
╘════════╧═════════╧═══════╧═════════╧═════════════════╛

Any help will be much appriciated.

petezurich
  • 9,280
  • 9
  • 43
  • 57
DonOfDen
  • 3,968
  • 11
  • 62
  • 112

2 Answers2

4

You need to convert the dict to a dataframe. Try this:

df = pd.DataFrame(board_dict)
print(tabulate(df.T, headers="keys"))

This prints out:

                 items    point
-------------  -------  -------
Done                 1        0
Doing                3       24
New                  2        0
Stuck                3       19
Ready to Test        1        1

If you need the sum as well try this:

df = pd.DataFrame(board_dict)
df = df.T
df.columns = ["_items", "_points"]
df = df.astype(int)
df.loc["Total"] = df.sum().T
print(tabulate(df, headers="keys"))

This prints out:

                 _items    _points
-------------  --------  ---------
Done                  1          0
Doing                 3         24
New                   2          0
Stuck                 3         19
Ready to Test         1          1
Total                10         44

Please note that items is not a good column name since it too is a Python function name. Therefore I changed it to _items.

I also changed this line in your dict 'point': Decimal('1') to this line 'point':'1'

petezurich
  • 9,280
  • 9
  • 43
  • 57
  • 1
    Wow! Perfect! I installed `pandas` and checked and it's perfect! Thank you. – DonOfDen Apr 02 '19 at 19:56
  • 1
    You're very welcome. Please see my edit regarding the row with the totals. Good luck with your project. – petezurich Apr 02 '19 at 19:59
  • 1
    the Decimal('1') is something which I am adding but the updated solution saved me more time. Thanks for the wishes, will share the project once finished.. it's an open source project :) – DonOfDen Apr 02 '19 at 20:05
2

You need to change the dictionary to one of the expected formats eg. a list of dictionaries: [{**v, 'column': k } for k, v in d.items() ] instead of the original dictionary

List of lists might be even easier to understand

tabulate( [[k, v['items'], v['point'] for k, v in d.items()],
           headers = ['columns', 'items', 'points'])

Dictionary of list is also supported:

    keys = d.keys()
    values = d.values() )          
    d2 = { 'columns': keys, 
           'items': [v['items'] for v in values], 
           'points': [v['point'] for v in values]
    }

    tabulate(d2, headers = 'keys')

Also If you have a problem with pseudographics from "fancy_grid", remove it. The sum you might need to add manually

Serge
  • 3,387
  • 3
  • 16
  • 34
  • i tested removing "fancy_grid", but its not helping... the output still the same.. yes, i can understand the sum should be a separate calculating.. @Serge – DonOfDen Apr 02 '19 at 19:49
  • I forgot to mention that you should mold the dictionary to a simpler format, clear to this simple tabulation library. – Serge Apr 02 '19 at 21:32