Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Construct pandas DataFrame from list of tuples of (row,col,values)

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"?

like image 248
gt6989b Avatar asked Nov 13 '13 18:11

gt6989b


People also ask

How do you create a pandas DataFrame from a list of tuples?

We need to send this list of tuples as a parameter to the pandas. DataFrame() function. The Pandas DataFrame object will store the data in a tabular format, Here the tuple element of the list object will become the row of the resultant DataFrame.

Can tuple of tuples be used to create DataFrame?

We can create a DataFrame from a list of simple tuples, and can even choose the specific elements of the tuples we want to use.

How do I create a column from a list in pandas?

Use the tolist() Method to Convert a Dataframe Column to a List. A column in the Pandas dataframe is a Pandas Series . So if we need to convert a column to a list, we can use the tolist() method in the Series . tolist() converts the Series of pandas data-frame to a list.

Can we create pandas DataFrame using dictionary of tuples?

So we can use strings, numbers (int or float), or tuples as keys. Values can be of any type. We can also pass a dictionary to the dataframe function. The keys will be column names and the values will form the columns.


1 Answers

You can pivot your DataFrame after creating:

>>> df = pd.DataFrame(data) >>> df.pivot(index=0, columns=1, values=2) # avg DataFrame 1      c1     c2 0                r1  avg11  avg12 r2  avg21  avg22 >>> df.pivot(index=0, columns=1, values=3) # stdev DataFrame 1        c1       c2 0                    r1  stdev11  stdev12 r2  stdev21  stdev22 
like image 102
Roman Pekar Avatar answered Oct 05 '22 15:10

Roman Pekar