2
for idx,ids in enumerate(uniq):
    SV = df_CenteredWin[df_CenteredWin['subVoyageIDs'] == ids]

    SV['minGroup']= np.isnan(SV.groupby(pd.TimeGrouper('30T')).DateTime.diff().dt.seconds)
    SV['groups'] = (SV['minGroup'].shift(1) != SV['minGroup']).astype(int).cumsum()
    SV_Noise = SV[SV['zScore_Noise'] == 'noise']
    uniqueID= SV_Noise.groups.unique()

    print(uniqueID, SV_Noise.subVoyageIDs.unique())

    for idx, groupid in enumerate(uniqueID):
        groups = SV[SV['groups'] == groupid]
        groups_nosie = groups[groups['zScore_Noise'] == 'noise']
        data = pd.DataFrame(data = { 'distance' : groups.Distance,
                       'Speed' : groups.Speed,
                        'Z-Score' :  groups.centeredZScore,
                         'flagged' :  groups.zScore_Noise.values})
        display(data.style.apply(lambda x: ['background: Yellow' if x.name == 'noise' else data for i in x]))     

can anyone explain me what is wrong in this line, and how can I correct it

display(data.style.apply(lambda x: ['background: Yellow' if x.name == 'noise' else data for i in x]))

I have this following data where i am trying to highlight the row where the flagged column is equal to 'noise'

 DateTime            Speed      Score        Distance   flagged
2011-01-09 12:21:59 1.840407   -0.845713    0.030673    noisefree
2011-01-09 12:23:00 4.883493    2.307917    0.082748    noisefree
2011-01-09 12:24:00 4.413968    1.752545    0.073566    noisefree
2011-01-09 12:24:59 4.950600    2.178342    0.081135    noisefree
2011-01-09 12:26:00 10.014879   4.355568    0.169697    noise
2011-01-09 12:27:00 7.534325    2.535460    0.125572    noisefree
2011-01-09 12:27:59 6.965328    2.122056    0.114154    noisefree
2011-01-09 12:29:00 6.993480    1.963185    0.118501    noisefree 

and the error is:

AttributeError: 'DataFrame' object has no attribute 'rstrip'
id101112
  • 1,012
  • 2
  • 16
  • 28
  • well I will say thar's kinda duplicate, but I posted because I tried that code and got some error, and I tried hard to find mistake which I did not find it, thanks for it – id101112 Nov 15 '18 at 05:13

1 Answers1

1

You were close. I'm not exactly sure why you got THAT error, but one issue was that you were returning your initial dataframe within the else block of your list comprehension.

If you replace that line with this, you may have better luck.

df.style.apply(lambda x: ["background: yellow" if v == "noise" else "" for v in x], axis = 1)

In this case, you are iterating over each row in your df, highlighting cells that equal noise.

Help from / possible duplicate of Conditionally format Python pandas cell

Edit: Ripping off @scott-boston and How to use Python Pandas Stylers for coloring an entire row based on a given column?,

def highlight_row(s,keyword,column):
    is_keyword = pd.Series(data=False, index=s.index)
    is_keyword[column] = s.loc[column] == keyword
    return ['background-color: yellow' if is_keyword.any() else '' for v in is_keyword]

df.style.apply(highlight_row, keyword="noise", column=["flagged"], axis=1)
Evan
  • 2,121
  • 14
  • 27
  • well I guess that was the mistake which was generating this error, in the else i was returning my initial DataFrame, which was wrong in this case, well thank you so much for correcting me, I have one more question, this is just highlighting one word, how can I highlight the full row? – id101112 Nov 15 '18 at 05:11
  • Something like this? https://stackoverflow.com/questions/43596579/how-to-use-python-pandas-stylers-for-coloring-an-entire-row-based-on-a-given-col – Evan Nov 15 '18 at 05:22
  • well thanks but, I already did it, thank you so much for your kind help – id101112 Nov 15 '18 at 05:32
  • that was really helpful since, you corrected my first mistake, thanks a lot – id101112 Nov 15 '18 at 05:35