Convert Pandas Dataframe To List Of Tuples - ('row', 'column', Value)
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 and bokeh My da
Solution 1:
You can reshape first by stack
and then convert to tuple
s:
tups = [tuple(x) for x in df.stack().reset_index().values.tolist()]
Another similar solution is create 3 levels MultiIndex
:
tups = df.stack().to_frame().set_index(0, append=True).index.tolist()
Or zip
3 separately array
s with numpy.repeat
, numpy.tile
and ravel
:
a = np.repeat(df.index, len(df.columns))
b = np.tile(df.columns, len(df))
c = df.values.ravel()
tups = list(zip(a,b,c))
Solution 2:
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) fork, v in res.items() fora, b in v.items()]
[('A', 'Foo', 1),
('A', 'Bar', 2),
('A', 'Bash', 3),
...
('D', 'Baz', 1)]
Solution 3:
With iterators and list comprehention:
my_list = []
forrowin df.iterrows():
my_list.extend([(row[0], i, v) for i, v inrow[1].iteritems()])
Post a Comment for "Convert Pandas Dataframe To List Of Tuples - ('row', 'column', Value)"