Indexing pandas dataframes inside pandas dataframes with python
Question: I have a series of dataframes inside a dataframe.

The top level dataframe is structured like this :

    
    
        24hr   48hr   72hr
    D1  x      x      x
    D2  x      x      x 
    D3  x      x      x
    

In each case x is a dataframe created with `pandas.read_excel()`

One of the columns in each x dataframe has the title 'Average Vessels Length'
and there are three entries (i.e. rows, indices) in that column.

What I want to return is the mean value for the column 'Average Vessels
Length'. I'm also interested in how to return an particular cell in that
column. I know there's a .mean method for pandas dataframes, but I can't
figure out the indexing syntax to use it.

Below is an example

    
    
    import pandas as pd
    
    a = {'Image name' : ['Image 1', 'Image 2', 'Image 3'], 'threshold' : [20, 25, 30], 'Average Vessels Length' : [14.2, 22.6, 15.7] }
    b = pd.DataFrame(a, columns=['Image name', 'threshold', 'Average Vessels Length'])
    
    c = pd.DataFrame(index=['D1','D2','D3'], columns=['24hr','48hr','72hr'])
    c['24hr']['D1'] = a
    c['48hr']['D1'] = a
    c['72hr']['D1'] = a
    c['24hr']['D2'] = a
    c['48hr']['D2'] = a
    c['72hr']['D2'] = a
    c['24hr']['D3'] = a
    c['48hr']['D3'] = a
    c['72hr']['D3'] = a
    

This returns the mean of the values in the column 'Average Vessels Length' :

    
    
    print b['Average Vessels Length'].mean()
    

This returns all the values in 24hr, D1, 'Average Vessels Length'

    
    
    print c['24hr']['D1']['Average Vessels Length']
    

This doesn't work :

    
    
    print c['24hr']['D1']['Average Vessels Length'].mean()
    

And I can't figure out how to access any particular value in
c['24hr']['D1']['Average Vessels Length']

Ultimately I want to take the mean from each column of Dx['Average Vessels
Length'].mean() and divide it by the corresponding D1['Average Vessels
Length'].mean()

Any help would be greatly appreciated.


Answer: I'm assuming that since you said each element of your big dataframe was a
dataframe, your example data should have been:

    
    
    import pandas as pd
    
    a = {'Image name' : ['Image 1', 'Image 2', 'Image 3'], 'threshold' : [20, 25, 30], 'Average Vessels Length' : [14.2, 22.6, 15.7] }
    b = pd.DataFrame(a, columns=['Image name', 'threshold', 'Average Vessels Length'])
    
    c = pd.DataFrame(index=['D1','D2','D3'], columns=['24hr','48hr','72hr'])
    c['24hr']['D1'] = b
    c['48hr']['D1'] = b
    c['72hr']['D1'] = b
    c['24hr']['D2'] = b
    c['48hr']['D2'] = b
    c['72hr']['D2'] = b
    c['24hr']['D3'] = b
    c['48hr']['D3'] = b
    c['72hr']['D3'] = b
    

To get the mean of each individual cell you can use `applymap`, which maps a
function to each cell of the DataFrame:

    
    
    cell_means = c.applymap(lambda e: e['Average Vessels Length'].mean())
    cell_means
    Out[14]: 
        24hr  48hr  72hr
    D1  17.5  17.5  17.5
    D2  17.5  17.5  17.5
    D3  17.5  17.5  17.5
    

And once you have those yo can get the column means etc. and go on to
normalize by the mean:

    
    
    col_means = cell_means.mean(axis=0)
    col_means
    Out[11]: 
    24hr    17.5
    48hr    17.5
    72hr    17.5
    dtype: float64
    

