【问题标题】:can somebody help me to correct my following code at the styling point in Pandas Python [duplicate]有人可以帮助我在 Pandas Python 的样式点更正我的以下代码 [重复]
【发布时间】:2019-04-17 22:41:55
【问题描述】:
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]))     

谁能解释一下这行有什么问题,我该如何纠正它

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

我有以下数据,我试图突出显示标记列等于“噪声”的行

 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 

错误是:

AttributeError: 'DataFrame' object has no attribute 'rstrip'

【问题讨论】:

  • 好吧,我会说 thar 有点重复,但我发布是因为我尝试了该代码并遇到了一些错误,我努力寻找我没有找到的错误,谢谢

标签: python pandas styles python-3.6


【解决方案1】:

你很亲密。我不确定您为什么会收到该错误,但一个问题是您在列表理解的 else 块中返回了初始数据帧。

如果你用这个替换那行,你可能会有更好的运气。

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

在这种情况下,您将遍历 df 中的每一行,突出显示等于 noise 的单元格。

来自Conditionally format Python pandas cell的帮助/可能重复

编辑: 扯掉@scott-boston 和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)

【讨论】:

  • 好吧,我猜这是产生这个错误的错误,否则我返回了我的初始数据帧,在这种情况下这是错误的,非常感谢你纠正我,我有一个更多问题,这只是突出一个词,我怎样才能突出整行?
  • 谢谢,但是我已经做到了,非常感谢您的帮助
  • 这真的很有帮助,你纠正了我的第一个错误,非常感谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-07
  • 2017-11-28
  • 2017-08-08
  • 1970-01-01
  • 2016-07-07
  • 2014-01-25
相关资源
最近更新 更多