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)

TA貢獻2051條經驗 獲得超10個贊
檢查你的數據:
ParserError: Error tokenizing data. C error: Expected 1 fields in line 53, saw 2
查找字符串中的分隔符。
您還可以嘗試: error_bad_lines= read_csv中的False參數
添加回答
舉報