0

Looking to create a table like this in Jupiter Notebook based on a pandas DataFrame. While styler has much on how to format data, much typically done in excel/word is formatting text to make it quickly readable. I'm aiming to cut excel/word out of process entirely and use pandas styler to summarize entire workflow. I have found limited documentation on how to format the text values/column widths/etc of data in index. I'm not familiar with HTML/CSS and am winging it. What I've tried seemed to have no impact on display of table. This is my data and some of code I've tried:

from faker import Faker
faker = Faker()
Faker.seed(4321)
dat = pd.DataFrame(
{'WorkFlow':[ 'Macro Economic Changes'
             ,'Industry Changes'
             ,'Federal Legislative Changes'
             ,'Model Development'
             ,'Model Validation'
             ,'Model Production Testing'
             ,'Market Research'
             ,'Marketing Plan'
             ,'Marketing Material'
             ,'Client Solicitation']})

choices    = ['Marketing','Management','Research']
conditions = [dat['WorkFlow'].str.find('Market')>-1
             ,dat['WorkFlow'].str.find('Change')>-1
             ,dat['WorkFlow'].str.find('Model' )>-1]
dat['Dept']= np.select(conditions,choices,'BusinessDev')
dat['AssignedAssociate'] =dat['WorkFlow'].apply(lambda x: faker.name())
dat['StatusReport']=np.array(['Green']*5+['Yellow']+['Green']*3+['Red'])
dat['StatusReport']= dat['StatusReport']+": Due to ...sdf asdfasf asdfasdfas asdgfasdfasdfsadfsadf. asdfsafsaf asdfsafsadf"

dat.set_index(['Dept','WorkFlow'],inplace=True)
dat

dat.style.set_table_styles(
#     { 'selector': 'th.col_heading'
#      , 'props'  : 'text-align: center;'                   },   
    {'selector' : 'th.index_name.'           
     , 'props'  : [('text-align','center')
                   ,('font-weight','bold')
                   ,('font-style','italic')
                   ,('width','2%')
                   ,('overflow-wrap','break-word')]} )
Jaime
  • 1
  • 1
  • What do you _want_ the output to look like? Can you mock something demonstrative of the format you're looking for? – Henry Ecker Jul 17 '21 at 00:31
  • I added an image link. Basically merging cells, centering, text wrapping and such. Not clear how to apply such formatting to pandas.Styler instances. Appreciate the help. – Jaime Jul 18 '21 at 04:39

1 Answers1

0

Merging of cells only happens for multiindexes:

e.g.:

df = pd.DataFrame(index=pd.MultiIndex.from_product([["Unit 1", "Unit 2"], ["Some Value", "Another"]], names=["Dept", "Workflow"]),
                  columns=["Comment 1", "Comment 2"],
                  data=[["Green", "Red"], ["Yellow", "Yellow"], ["Green", "Red"], ["Yellow", "Yellow"]])
styler = df.style
styler

enter image description here

You can do some basic styling of the data cells and index cells like this:

styler.set_table_styles([{'selector': 'td', 'props': 'text-align: left;'},
                         {'selector': 'th.level1', 'props': 'text-align: left;'}])\
      .applymap(lambda x: f"color: {x.lower()}")

enter image description here

Attack68
  • 4,437
  • 1
  • 20
  • 40