Finding the Maximum Value Column for Each Row
Consider a DataFrame with multiple columns containing numerical values. To identify the column that holds the maximum value for each row, implement the following steps:
Use the idxmax function along with the axis=1 parameter to determine the column where the maximum value occurs in each row:
column_with_max_value = df.idxmax(axis=1)
To create a new column labeled 'Max' that contains the column labels associated with the maximum values, assign it as such:
df['Max'] = column_with_max_value
The resulting DataFrame will include an additional column labeled 'Max', which indicates the column holding the greatest value for each row:
print(df)
Additional Notes:
The above is the detailed content of How to Find the Column with the Maximum Value for Each Row in a DataFrame?. For more information, please follow other related articles on the PHP Chinese website!