亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

突出顯示數據框中按順序排列的前 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 關注
  • 180 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號