首頁 > 後端開發 > Python教學 > 如何找到 DataFrame 中每行的最大列值?

如何找到 DataFrame 中每行的最大列值?

Patricia Arquette
發布: 2024-11-15 01:20:02
原創
553 人瀏覽過

How to Find the Maximum Column Value for Each Row in a DataFrame?

找出DataFrame 中的最大列值

要辨識DataFrame 中每行具有最大值的列,請依照下列步驟操作:

使用idxmax() 和axis=1:

df.idxmax(axis=1)
登入後複製
登入後複製

這將傳回一個包含每行最大值的列標籤的系列。

為最大值建立一個新欄位:

找到最大欄位值後,使用系列建立一個名為「Max」的新欄位:

df['Max'] = df.idxmax(axis=1)
登入後複製
登入後複製

範例:

給定以下DataFrame:

Communications and Search Business General Lifestyle
0.745763 0.050847 0.118644 0.084746
0.333333 0.000000 0.583333 0.083333
0.617021 0.042553 0.297872 0.042553

運行程式碼:

df.idxmax(axis=1)
登入後複製
登入後複製

將產生以下輸出:

0    Communications and Search
1          Business
2    Communications and Search
3    Communications and Search
4          Business
dtype: object
登入後複製

將其新增為新欄位「Max」:

df['Max'] = df.idxmax(axis=1)
登入後複製
登入後複製

將產生以下輸出:

Communications and Search Business General Lifestyle Max
0.745763 0.050847 0.118644 0.084746 Communications and Search
0.333333 0.000000 0.583333 0.083333 Business
0.617021 0.042553 0.297872 0.042553 Communications and Search

以上是如何找到 DataFrame 中每行的最大列值?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板