如何計算 Pandas 中多列的最大值?

Susan Sarandon
發布: 2024-10-17 20:53:30
原創
776 人瀏覽過

How Can I Calculate the Maximum Value Across Multiple Columns in Pandas?

Find the Maximum Value Across Multiple Columns in Pandas

Suppose you have a dataframe with several columns and wish to create a new column containing the maximum value from two or more existing columns. For instance, given columns A and B, you need to create a column C where:

C = max(A, B)
登入後複製

To accomplish this task:

  1. Use the max function along with axis=1 to calculate the maximum value for each row across the specified columns:
df[["A", "B"]].max(axis=1)
登入後複製
  1. Assign the result to a new column:
df["C"] = df[["A", "B"]].max(axis=1)
登入後複製

This generates a new column C containing the maximum value for each row between columns A and B:

A B C
1 -2 1
2 8 8
3 1 3

Note that this technique can be generalized to find the maximum value across any number of columns.

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

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