为了账号安全,请及时绑定邮箱和手机立即绑定

突出显示数据框中按顺序排列的前 10 条记录

突出显示数据框中按顺序排列的前 10 条记录

肥皂起泡泡 2023-08-15 16:52:54
需要帮助突出显示排名最高的前 10 家商店。销售额import pandas as pdimport numpy as npimport matplotlib.pyplot as plt% matplotlib inline import seaborn as sns store = pd.read_csv("https://github.com/Kevin-ck1/Intro-To-Data-Science/blob/master/Clothing.csv")store.head()数据集的变量名如下图tsale   sales   margin  nown    nfull   npart   naux    hoursw  hourspw     inv1    inv2    ssize   start
查看完整描述

2 回答

?
炎炎设计

TA贡献1808条经验 获得超4个赞

当然!我们可以通过首先找到排名前 10 的商店的索引来做到这一点。完成此操作后,我们将样式函数应用于每一行,并检查当前行索引(此处存储在 中row.name)是否位于先前确定的前 10 个商店的索引中。如果是:我们返回一个突出显示该行的列表,如果不是:我们根本不设置该行的样式。


def highlight_top(df, n=1):

    def _highlight_top(row, index):

        if row.name in index:

            return ["background-color: yellow" for _ in row]

        return ["" for _ in row]

    

    top_stores = df.nlargest(n, "sales")

    top_idx = top_stores.index.values

    return df.style.apply(_highlight_top, index=top_idx, axis=1)

    

# subset our data for testing purposes by only taking the first 10 rows

test_data = store.head(10)


# highlight the top 5 stores in terms of sales

highlight_top(test_data, n=5)

https://img1.sycdn.imooc.com//64db3d4c000171a706520257.jpg

查看完整回答
反对 回复 2023-08-15
?
侃侃无极

TA贡献2051条经验 获得超10个赞

检查你的数据:

ParserError: Error tokenizing data. C error: Expected 1 fields in line 53, saw 2

查找字符串中的分隔符。

您还可以尝试: error_bad_lines= read_csv中的False参数


查看完整回答
反对 回复 2023-08-15
  • 2 回答
  • 0 关注
  • 93 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信