python - 突出显示前 10 名,按数据框中的顺序记录
问题描述
需要帮助突出显示数量最多的前 10 家商店。销售额
import pandas as pd
import numpy as np
import 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
解决方案
当然!我们可以通过首先找到前 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)
推荐阅读
- javascript - 如何使用这些限制验证我的输入字段
- arrays - 这个方法的返回类型是切片还是借来的数组?
- javascript - 在追加期间需要帮助重新排列 Google 表格列
- matlab - MATLAB:基于两个变量找到目标值的有效方法
- scipy - 使用 scipy.stats 在 customPDF 类中设置种子
- spring-mvc - 使用 Postman 测试登录 api Spring Security
- flutter - Flutter:如何将小部件保存为具有透明背景的 png?
- html - 使用带有 Django 模板 {%staric 'img/header/header-1.jpg%} 的内联 url() 属性
- google-play-console - 在 Google Play 控制台中访问“API 访问”需要哪些权限?
- vuejs3 - 我正在尝试注册一个组件,但我收到错误“Vue' is not defined no-undef”