0

I have an two pandas dataframe like this:

Df1:

Name Age Hobby
ABC  23  Reading
GHI  25  Playing

DF2:

Name    Age      Hobby
Green  Yellow    Green 
Green  NaN       Red

What I am looking is 3rd data-frame which makes a df in such a way that:

  1. ABC and GHI are colured in Green
  2. 23 is in Yellow and 25 remains white as it is Nan
  3. Reading in Green and Playing in Red

Any help on the same

1 Answer 1

3

Use styles with custom function:

def color(x): 
   c = 'background-color: '
   return Df2.apply(lambda x: x.str.lower()).radd(c).fillna('')


Df1.style.apply(color,axis=None).to_excel('styled.xlsx', engine='openpyxl', index=False)

Output enter image description here

Sign up to request clarification or add additional context in comments.

15 Comments

is this new df will be in excel or a pandas dataframe ?
display(df1.style.apply(color,axis=None)) will return it in the console @RahulAgarwal. Good one jez, upvoted
@RahulAgarwal i edited the post with my results using this code. :)
Ok thanks!! I will try if not...ask a new question!! Thanks for all the help!!
@RahulAgarwal - Btw, styler object working best in anaconda notebooks, so if possible, use it there.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.