Python Forum
how do you style data frame that has empty rows.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how do you style data frame that has empty rows.
#1
I have this data frame df:


I need to style this data frame based on Percent_Utilized column. I have this so far:

I have this data frame:

df

Server        Env.    Model   Percent_Utilized
server123     Prod     Cisco.       50
server567.    Prod     Cisco.       80
serverabc.    Prod     IBM.         100
serverdwc.    Prod     IBM.         45
servercc.     Prod      Hitachi.    25
Avg                                60

server123Uat  Uat     Cisco.       40
server567u    Uat     Cisco.       30
serverabcu    Uat     IBM.         80
serverdwcu    Uat     IBM.         45
serverccu     Uat     Hitachi      15
Avg                               42
I need to apply style to this data frame based on Percent_Utilized column. I have this solution so far:

def color(val):
  if pd.isnull(val):
      return
  elif val > 80:
      background_color = 'red'
  elif val > 50 and val <= 80:
      background_color = 'yellow'
  else:
      background_color = 'green'
  return 'background-color: %s' % background_color

def color_for_avg_row(row):
    styles = [''] * len(row)
    if row['Server'] == 'Avg':
        if row['Percent_Utilized'] > 80:
            color = 'background-color: red'
        elif row['Percent_Utilized'] > 50:
            color = 'background-color: yellow'
        else:
            color = 'background-color: green'
        styles = [color for _ in row.index]
    return pd.Series(styles, index=row.index)
df_new = (df.style
              .apply(color_for_avg_row, axis=1)
              .applymap(color, subset=["Percent_Utilized"]))
df_new [/inline]

this set of code works when there is no empty rows. But there is an empty row in df and I need to keep it to separate each group by env column.

I am currently getting this error:

AttributeError: 'NoneType oject has no attribute 'rstrip'.

Any ideas?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  googletrans library to translate text language for using data frame is not running gcozba2023 0 1,237 Mar-06-2023, 09:50 AM
Last Post: gcozba2023
  (Python) Pulling data from UA Google Analytics with more than 100k rows into csv. Stockers 0 1,241 Dec-19-2022, 11:11 PM
Last Post: Stockers
  How to properly format rows and columns in excel data from parsed .txt blocks jh67 7 1,890 Dec-12-2022, 08:22 PM
Last Post: jh67
  Load multiple Jason data in one Data Frame vijays3 6 1,560 Aug-12-2022, 05:17 PM
Last Post: vijays3
  conditionals based on data frame mbrown009 1 905 Aug-12-2022, 08:18 AM
Last Post: Larz60+
  Showing an empty chart, then input data via function kgall89 0 985 Jun-02-2022, 01:53 AM
Last Post: kgall89
  Merging two Data Frame on a special case piku9290dgp 0 1,093 Mar-02-2022, 10:43 AM
Last Post: piku9290dgp
  Save data frame to .csv df.to.csv() mcva 1 1,545 Feb-03-2022, 07:05 PM
Last Post: mcva
  Move a particular row in pandas data frame to last row klllmmm 0 3,776 Dec-27-2021, 09:11 AM
Last Post: klllmmm
  The code I have written removes the desired number of rows, but wrong rows Jdesi1983 0 1,636 Dec-08-2021, 04:42 AM
Last Post: Jdesi1983

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020