Getting the Maximum Value from Multiple DataFrame Columns
When working with dataframes, having a consolidated column containing the maximum values from multiple other columns can be useful. One such instance is obtaining the maximum value between columns A and B, as illustrated in this example:
import pandas as pd df = pd.DataFrame({"A": [1, 2, 3], "B": [-2, 8, 1]}) print("Original DataFrame:") print(df)
Our goal is to create a new column C containing the maximum value for each row between columns A and B. To achieve this, we can use the following steps:
print("\nMaximum values between A and B:") df["C"] = df[["A", "B"]].max(axis=1) print(df)
Explanation:
As a result, we obtain a modified dataframe with the "C" column containing the desired maximum values:
Original DataFrame: A B 0 1 -2 1 2 8 2 3 1 Maximum values between A and B: A B C 0 1 -2 1 1 2 8 8 2 3 1 3
The above is the detailed content of How to Combine Maximum Values from Multiple Columns in a DataFrame?. For more information, please follow other related articles on the PHP Chinese website!