Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
301 views
in Technique[技术] by (71.8m points)

python - 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 data is being read in as an Excel file into a dataframe to something along the lines of:

    Foo    Bar    Bash    Baz   ...
A   1      2      3       4
B   2      1      0       3
C   0      0      2       0
D   2      3      5       1
...

The documentation says The data for a HeatMap may be supplied as 2D tabular data with one or more associated value dimensions.

Plotting the dataframe itself doesn't work, I feel like I need to get my data into a form like:

[('A', 'Foo', 1), ('A', 'Bar', 2), ('A', 'Bash', 3), ('A', 'Baz', 4), ('B', 'Foo', 1)...]

Is there a faster way to do this than manually iterating through the entire dataframe and building it manually?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...