Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (19.9k points)

I found one thread of converting a matrix to das pandas DataFrame. However, I would like to do the opposite - I have a pandas DataFrame with time series data of this structure:

row time stamp, batch, value

1, 1, 0.1

2, 1, 0.2

3, 1, 0.3

4, 1, 0.3

5, 2, 0.25

6, 2, 0.32

7, 2, 0.2

8, 2, 0.1

...

What I would like to have is a matrix of values with one row belonging to one batch:

[[0.1, 0.2, 0.3, 0.3],

[0.25, 0.32, 0.2, 0.1],

...]

which I want to plot as heatmap using matplotlib or alike.

1 Answer

0 votes
by (25.1k points)

if you know that you get exactly 4 values for every unique value of batch you can just use the matrix directly. Like this:

df = df.sort_values("batch")

my_indices = [1, 2] 

mtr = df.values[:, my_indices] 

mtr = mtr.reshape(-1, 4) 

group by the desired index using the groupby method like:

g = df.groupby("batch")

And then convert this group to an array by aggregating using the list constructor. The result can then be converted to an array using the .values property

mtr = g.aggregate(list).values

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...