1
import pandas as pd
test_df =pd.DataFrame({"col1":[1,12,3,4],
            "col2":[3,14,5,6],
             "col3":[4,5,6,7]})

print(test_df)
   col1  col2  col3
0     1     3     4
1    12    14     5
2     3     5     6
3     4     6     7

def highlight(row):
    ret =["" for _ in row.index]   
    if row['col3'] == 5:                                                                         
        ret[row.index.get_loc('col3')] ="background-color: #f2f20a"
    if row['col2'] == 5:                                                                         
        ret[row.index.get_loc('col2')] ="background-color: #f2f20a"
        
    return ret
dd= test_df.style.apply(highlight, axis=1)
print(dd)
    col1 col2   col3
0   1    3      4
1   12  14      **5**
2   3   **5**   6
3   4   6       7

How can I separate create dataframe or excel which has only a highlight row only? In this case, only rows 1, 2 will be come in separate excel or dataframes.

Thanks in Advance!

kundan kaushik
  • 217
  • 2
  • 10
  • do you want select rows 1,2 to create a new DataFrame? what is the question here? –  Feb 21 '22 at 08:32

1 Answers1

1

Use:

m = (pd.concat([(test_df['col2'] == 5), 
                   (test_df['col3'] == 5)], axis=1)
          .reindex(test_df.columns, fill_value=False, axis=1))
print (m)
    col1   col2   col3
0  False  False  False
1  False  False   True
2  False   True  False
3  False  False  False

def highlight(x):
    c = "background-color: #f2f20a"

    # DataFrame of styles
    df1 = pd.DataFrame('', index=x.index, columns=x.columns)
    # set columns by condition
    df1 = df1.mask(m, c)
    return df1


df1 = test_df[m.any(axis=1)]
print (df1)
   col1  col2  col3
1    12    14     5
2     3     5     6

Original solution:

def highlight(x):

    # DataFrame of styles
    df1 = pd.DataFrame('', index=x.index, columns=x.columns)
    # set columns by condition
    df1.loc[x['col2'] == 5, 'col2'] = "background-color: #f2f20a"
    df1.loc[x['col3'] == 5, 'col3'] = "background-color: #f2f20a"

    return df1


test_df.style(highlight, axis=None)
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • Thank so much for your reply jezrael. I have to create another data frame that has only highlight records. The above code gives all record. My pasted code is in running status. only not able to create a filter record which has applied colour. – kundan kaushik Feb 21 '22 at 09:19
  • I Need to filter the record which has colour applied. – kundan kaushik Feb 21 '22 at 09:26
  • Thank you Jezael for your help. here i need to create data frame which has only colour record. – kundan kaushik Feb 21 '22 at 09:29
  • 1
    @kundankaushik - more complicated like seems. Possible solution is change logic - first create mask in variable `m` and it is used for highlight values and then for filtering to `df1` – jezrael Feb 21 '22 at 09:38
  • @kundankaushik - answer was edited. – jezrael Feb 21 '22 at 09:38
  • the earlier posted solution was look fine for my problem because I just created a pseudo-code to understand the problem. My original data frame has more than 30 columns and different condition I have to apply to colouring the columns. if we can add logic in the previous method(highlight) which you aldy erased that would be good for me for use. Once again Thanks so much. – kundan kaushik Feb 21 '22 at 10:02
  • 1
    @kundankaushik - added to answer. So not necessary create `df1` ? – jezrael Feb 21 '22 at 10:04
  • @ jezrael: Can I filter colour record while applying the colour on record so that we can get a seperate data frame that has an only colour record. – kundan kaushik Feb 21 '22 at 10:07
  • original solution providing all records. can we separate the record here itself? those have only colour columns – kundan kaushik Feb 21 '22 at 10:09
  • 1
    @kundankaushik - so need first highlight and then filter? I think not possible. – jezrael Feb 21 '22 at 10:10
  • 1
    @kundankaushik - I found solution [this](https://stackoverflow.com/a/59937651/2901002) - but need StyleFrame, not pandas DataFrame. It should be way if need it. – jezrael Feb 21 '22 at 10:13
  • Thank you @ jezrael :) .Your solution is correct but in my problem it does not fit. anyway thanks a lot for your help – kundan kaushik Feb 23 '22 at 06:26
  • 1
    @kundankaushik - I think need new question only about `StyleFrame`. In pandas is not possible working with styles like `StyleFrame`. – jezrael Feb 23 '22 at 06:27
  • @ jezrael sir, i post one similar question can you please look on it. Title is :How to get all df inside apply function of pandas – kundan kaushik Feb 23 '22 at 06:37
  • Sir, Please look on this question https://stackoverflow.com/q/71232311/8241863. – kundan kaushik Feb 23 '22 at 07:43