Python Forum

Full Version: An Object of Objects?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Bare with me, relatively new to playing with python, I'm trying to backwards engineer an object so that I can recreate it slightly differently. I'm trying to understand what creates this structure.

The object in the code I'm working with is called s

So... if I type
print type(s)
I get the following results:
Output:
<class 'pandas.core.series.Series'> <class 'pandas.core.series.Series'> <class 'pandas.core.series.Series'> <class 'pandas.core.series.Series'> <class 'pandas.core.series.Series'> <class 'pandas.core.series.Series'> <class 'pandas.core.series.Series'> <class 'pandas.core.series.Series'> <class 'pandas.core.series.Series'> <class 'pandas.core.series.Series'> <class 'pandas.core.series.Series'> <class 'pandas.core.series.Series'> <class 'pandas.core.series.Series'> <class 'pandas.core.series.Series'>
What is this? it isn't a list... it isn't a tuple...
What kind of object returns multiple objects?
Added points for how do I make one?

I am working with a pandas dataframe iterating through the columns and outputting them to series. I need all of those series as one (nested?) object matching what I'm seeing in s
You need to post the code that defines s. It appears s is a class with 14 instances.
s represents self in the apply found here:

def background_gradient(s, m, M, cmap='RdBu'):
    print type(s)
    if M > -1*m:
      max = M
    else: max = -1*m
    rng = max
    norm = colors.Normalize(-2*max,
                            2*max)
    normed = norm(s.values)
    c = [colors.rgb2hex(x) for x in plt.cm.get_cmap(cmap)(normed)]
    return ['background-color: %s' % color for color in c]

...


counts_table.style.applymap(color_negative_red)\
   .apply(background_gradient,
               cmap='RdBu',
               m=df['Dataframe Value'].min().min(),
               M=df['Dataframe Value'].max().max()
               )\
   .highlight_null(null_color='#F4F6F8')
I thought it may be a class of some sort. I've never had to create a class, but when I started playing with it, I couldn't seem to find a way that print class would give the desired outcome; at best I would need to use print class.variable but this leads to having to call each variable independently, where I need to be able to get them all at once. However you say the class has instances, which sounds different than nested objects.
I forgot my libraries:

import pandas as pd
import numpy as np 
import matplotlib.pyplot as plt
from matplotlib import colors

Okay, here's the full code and the context of what I'm trying to do:
import pandas as pd
import numpy as np 
import matplotlib.pyplot as plt
from matplotlib import colors

def background_gradient(s, m, M, cmap='RdBu'):
    if M > -1*m:
      max = M
    else: max = -1*m
    rng = max
    norm = colors.Normalize(-2*max,
                            2*max)
    normed = norm(s.values)
    c = [colors.rgb2hex(x) for x in plt.cm.get_cmap(cmap)(normed)]
    return ['background-color: %s' % color for color in c]
    
def color_negative_red(val):
    color = 'black'
    if val < 0: color = '#BF0711'
    elif val >= 0: color = '#212B36'
    else: color = '#DFE3E8'
    return 'color: %s' % color
    
df = datasets["Topic Counts and AVG Deviations"]
counts_table = pd.pivot_table(df, values='count', index=['ticket about tag'],columns=['datestamp'], aggfunc=np.sum)
deviation_table =  pd.pivot_table(df, values='AVG Deviation', index=['ticket about tag'],columns=['datestamp'], aggfunc=np.sum)

counts_table.style.applymap(color_negative_red)\
   .apply(background_gradient,
               cmap='RdBu',
               m=df['AVG Deviation'].min().min(),
               M=df['AVG Deviation'].max().max()
               )\
   .highlight_null(null_color='#F4F6F8')
When creating the output heatmap, the color of the table cell is based on the value of the cell. What I wanted to do was to rather than have it based on the 'count' value which is numerically displayed, I wanted to use the 'AVG deviation' value to define the cell coloring.

Being that the apply function seems to do something to the 'self' to turn it into a series, I thought that if I could recreate the series from the data in the deviation_table, I could substitute 's' in the background_gradients with my manually created series.

However, with the way type(s) returns multiple series objects and the fact that I can't figure out how to do this; I'm open to other suggestions on how to replace those values for the background coloring only, while still displaying the numeric 'count' value in the cell.

I hope that makes sense.
I've realized the error in my ways. Ignore all of the above.

To give more explanation, I was thinking it was somehow importing all of the class objects in s once, not iterating through. :embarassed: