Convert pandas dataframe to list of tuples - ('Row', 'Column', Value)

后端 未结 3 1507
谎友^
谎友^ 2021-01-07 12:17

There are a few other questions regarding the same subject, but the format desired is different in all.

I am trying to build a heatmap visualization using holoviews

3条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-07 12:49

    Using pd.DataFrame.to_dict:

    res = df.to_dict('index')
    
    {'A': {'Bar': 2, 'Bash': 3, 'Baz': 4, 'Foo': 1},
     'B': {'Bar': 1, 'Bash': 0, 'Baz': 3, 'Foo': 2},
     'C': {'Bar': 0, 'Bash': 2, 'Baz': 0, 'Foo': 0},
     'D': {'Bar': 3, 'Bash': 5, 'Baz': 1, 'Foo': 2}}
    

    Then via a list comprehension:

    lst = [(k, a, b) for k, v in res.items() for a, b in v.items()]
    
    [('A', 'Foo', 1),
     ('A', 'Bar', 2),
     ('A', 'Bash', 3),
     ...
     ('D', 'Baz', 1)]
    

提交回复
热议问题