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:
df[["A", "B"]].max(axis=1)
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中文網其他相關文章!