0

I have a dataframe like this:

name       color parking_space
0  Terminal 1, 2   Green Lot            40
1     Terminal 4    Blue Lot            81
2     Terminal 5  Yellow Lot            59
3     Terminal 7  Orange Lot            45
4     Terminal 8     Red Lot            31
5      Long-Term         Lot            55

This is the data scraped using selenium.I want to change the background colour of the entire row based on the value of parking_space. here is the code that i have tried.But the output of this is the dataframe which is the same old database. code:

terminal_name=driver.find_elements_by_class_name("tp-h-mod")
terminal_color=driver.find_elements_by_class_name("terminals-lot")
terminal_capacity=driver.find_elements_by_class_name("terminal-percentage")
mylist=[]
mylist1=[]
mylist2=[]
list1=[]
for data in terminal_name:
        mylist.append(data.text)
for data in terminal_color:
        mylist1.append(data.text)
for data in terminal_capacity:
        mylist2.append(data.text)
for i in range(6):
    text=mylist2[i]
    m=text.split('%')[0]
    list1.append(m)
df=pd.DataFrame({'name':mylist,'color':mylist1,'parking_space':list1})
def highlight(row):
    if int(row.parking_space[:2]) <= 25:
        return ['background-color: green']*3
    elif int(row.parking_space[:2]) >=26 and int(row.parking_space[:2]) <=50:
        return ['background-color:yellow']*3
    else:
        return ['background-color:red']*3
df.style.apply(highlight, axis=1)
print(df)
path = 'C:/Users/InterCEP/Desktop/parking/'
df.to_html(path+'parking.html')
  • In the [pandas documentation](https://pandas.pydata.org/pandas-docs/stable/style.html#Fun-stuff) there's a whole section on styling your dataframes. What have you tried so far? – G. Anderson Oct 04 '18 at 15:43
  • Please edit your post with the code, rather than adding a comment, so it's easier to read. When you run that code, what is the output, and how does that compare with what you want? – G. Anderson Oct 04 '18 at 15:57
  • @G.Anderson I edited my question.When i try printing the dataframe it again gives me the old dataframe with no formatting – Soniya Chawla Oct 04 '18 at 16:02
  • What are the `type()` values in your `parking_space` column? As near as I can tell, you are likely trying to apply numeric comparison to string values, e.g., `'30% Full' <= 25` which will never evaluate to `True` – G. Anderson Oct 04 '18 at 16:07
  • @G.Anderson Yes the type() is String.I have made another list and extracted the numerical values from parking_space(list1).How can i use that list1 here? – Soniya Chawla Oct 04 '18 at 16:11
  • I have edited the code above with the changes and typecasting.Still the result is the same dataframe with no formatting. @G.Anderson – Soniya Chawla Oct 04 '18 at 16:22

1 Answers1

2

So, once you solve the str/int comparison, with something like int(row['parking_space'][:2]), it looks like this has been solved in this question.

Using a not-accepted answer from that question:

def highlight(row):
    if int(row.parking_space[:2]) <= 25:
        return ['background-color: green']*3
    elif int(row.parking_space[:2]) >=26 and int(row.parking_space[:2]) <=50:
        return ['background-color:yellow']*3
    else:
        return ['background-color:red']*3

Then, to save the styled dataframe, you assign a variable as below

df_styled=df.style.apply(highlight, axis=1)

df_styled is now a <pandas.io.formats.style.Styler objectthat generates a string of self-contained CSS and HTML.

If you then call df_styler.render() you get the HTML ready to be saved as plaintext and rendered by a browser:

<style  type="text/css" >
    #T_20b96880_c80e_11e8_a82d_a0afbd198380row0_col0 {
            background-color: yellow;
            ...
G. Anderson
  • 5,815
  • 2
  • 14
  • 21
  • Edited with solution – G. Anderson Oct 04 '18 at 17:03
  • Again no luck.I am getting the same data frame @G. Anderson – Soniya Chawla Oct 04 '18 at 17:18
  • What environment are you outputting to? This works in my jupyter environment, but pandas stylers are HTML based so you might need additional steps – G. Anderson Oct 04 '18 at 17:23
  • If you want the raw html in table format, you can save the styler to a variable then call `.render()` on it to output the table, as found [here](https://stackoverflow.com/questions/40421893/how-do-i-use-pandas-dataframe-style) – G. Anderson Oct 04 '18 at 17:27
  • you can also [export the styling to excel](http://pandas-docs.github.io/pandas-docs-travis/style.html#Export-to-Excel) – G. Anderson Oct 04 '18 at 17:32
  • I am outputing to jupyter only.I want to convert the dataframe to a html table later.I have edited my question with the complete code that i am trying. – Soniya Chawla Oct 04 '18 at 19:37
  • This looks like a quirk of the styler. Because a pandas dataframe is just a list of object references in memory, there's no mechanism to inherently save the formatting with the object. Hence, when you call the object or print it again, all of the style has disappeared. I've edited my post with the steps to get the HTML for the formatted table into a usable format. Rather than writing the df to a file with `to_html` directly, you need to write the styler object to the file – G. Anderson Oct 04 '18 at 19:51
  • No problem! And thank you for asking, because I just learned a bunch about dataframe styling! :) – G. Anderson Oct 04 '18 at 20:07
  • @G.Anderson. Thanks for your solution. Just in case someone does not understand that why you multiply 3 with the returned color list. I found it is because there are three columns/cells for each row – Jason LiLy Feb 20 '19 at 19:04