I have a list of tuples like
data = [
('r1', 'c1', avg11, stdev11),
('r1', 'c2', avg12, stdev12),
('r2', 'c1', avg21, stdev21),
('r2', 'c2', avg22, stdev22)
]
and I would like to put them into a pandas DataFrame with rows named by the first column and columns named by the 2nd column. It seems the way to take care of the row names is something like pandas.DataFrame([x[1:] for x in data], index = [x[0] for x in data])
but how do I take care of the columns to get a 2x2 matrix (the output from the previous set is 3x4)? Is there a more intelligent way of taking care of row labels as well, instead of explicitly omitting them?
EDIT It seems I will need 2 DataFrames - one for averages and one for standard deviations, is that correct? Or can I store a list of values in each "cell"?
You can pivot your DataFrame after creating:
I submit that it is better to leave your data stacked as it is:
Then it's a bit more intuitive to say
This way it is implicit that you're seeking to reshape the averages, or the standard deviations. Whereas, just using
pivot
, it's purely based on column convention as to what semantic entity it is that you are reshaping.This is what I expected to see when I came to this question:
gives