知识点
1.找出每一行最大值和对应的列索引:
#找出每行最大值对应的索引
df['max_idx']=df.idxmax(axis=1)
#取出该最大值
df['max_val']=df.max(axis=1)
2.找出每一行最小值和对应的列索引:
# 找出每行最小值对应的索引
df['min_idx']=df.idxmin(axis=1)
# 取出该最小值
df['min_val']=df.min(axis=1)
代码测试
# -*- coding: utf-8 -*-
import pandas as pdif __name__ == "__main__":dicData = {'t_2014': [123, 564, 896], 't_2015': [451, 751, 351], 't_2016': [242, 127, 356]}df_data = pd.DataFrame(dicData)print(df_data)df_max_min = pd.DataFrame()df_max_min['max'] = df_data.max(axis=1)df_max_min['min'] = df_data.min(axis=1)print(df_max_min)df_result = pd.concat([df_data, df_max_min], axis=1, join='outer', ignore_index=False)print(df_result)
运行结果: