Python 格式化最大值和索引值的输出
下面的代码输出指定列Python 格式化最大值和索引值的输出,python,pandas,database,dataframe,formatting,Python,Pandas,Database,Dataframe,Formatting,下面的代码输出指定列max\u列和min\u列的最大值及其索引。我想以预期结果的格式打印它。其中最大值及其索引位于同一行。如何安排max_值\u STD\u Q1、max_索引\u STD\u Q1,以获得预期结果 input.csv文件: element,LNPT,SNPT,NLP,NSP,TNT,TPnL,MxPnL,MnPnL,MxU,MxD [ 2. 2. 30.],0,0,4,4,8,-0.1,-0.0,-0.1,17127,-3 [ 2. 2. 40.],0,0,2,2,4,0.
max\u列
和min\u列
的最大值及其索引。我想以预期结果的格式打印它。其中最大值及其索引位于同一行。如何安排max_值\u STD\u Q1、max_索引\u STD\u Q1
,以获得预期结果
input.csv文件:
element,LNPT,SNPT,NLP,NSP,TNT,TPnL,MxPnL,MnPnL,MxU,MxD
[ 2. 2. 30.],0,0,4,4,8,-0.1,-0.0,-0.1,17127,-3
[ 2. 2. 40.],0,0,2,2,4,0.0,-0.0,-0.0,17141,-3
[ 2. 2. 50.],0,0,2,2,4,0.0,-0.0,-0.0,17139,-3
[ 2. 2. 60.],2,0,6,6,12,0.5,2.3,-1.9,17015,-3
[ 2. 2. 70.],1,0,4,4,8,0.3,0.3,-0.0,17011,-3
代码:
输出:
Max values for STDOutputs_Q1:
LNPT 1777.0
SNPT 1677.0
NLP 7410.0
MxPnL 97.9
NSP 7410.0
TNT 14820.0
TPnL 853.1
MxU 26060.0
dtype: float64
Index:
LNPT 215
SNPT 211
NLP 214
MxPnL 56175
NSP 214
TNT 214
TPnL 6842
MxU 1
预期产出:
LNPT 1777.0 Index: 215
SNPT 1677.0 Index: 211
NLP 7410.0 Index: 214
MxPnL 97.9 Index: 56175
NSP 7410.0 Index: 214
TNT 14820.0 Index: 214
TPnL 853.1 Index: 6842
MxU 26060.0 Index: 1
dtype: float64
您可以将
max\u value\u STD\u Q1
和max\u index\u STD\u Q1
合并,然后重命名列。将信息添加到列max\u index
df=pd.concat([max_values_STD_Q1,max_index_STD_Q1],轴=1)
df.columns=['max_value','max_index']
df['max_index']='index:'+df['max_index'].astype(str)
两个函数都使用转置:
编辑:
是否还有一种方法可以将索引视为元素值,从而将
LNPT
的索引改为3,而是[2.2.60.]
@tonyselcuk-是的,您可以尝试设置索引
类似于编辑中的答案。
LNPT 1777.0 Index: 215
SNPT 1677.0 Index: 211
NLP 7410.0 Index: 214
MxPnL 97.9 Index: 56175
NSP 7410.0 Index: 214
TNT 14820.0 Index: 214
TPnL 853.1 Index: 6842
MxU 26060.0 Index: 1
dtype: float64
# print(df)
max_value max_index
LNPT 2.0 Index: 3
SNPT 0.0 Index: 0
NLP 6.0 Index: 3
MxPnL 2.3 Index: 3
MnPnL -0.0 Index: 1
MxD -3.0 Index: 0
df = pd.read_csv('input.csv')
max_columns = ['LNPT', 'SNPT', 'NLP', 'MxPnL', 'MnPnL', 'MxD'] # named indexes
df1 = df[max_columns].agg(['max','idxmax']).T
df1.columns = ['max_value', 'max_index']
print (df1)
max_value max_index
LNPT 2.0 3.0
SNPT 0.0 0.0
NLP 6.0 3.0
MxPnL 2.3 3.0
MnPnL -0.0 1.0
MxD -3.0 0.0
max_columns = ['LNPT', 'SNPT', 'NLP', 'MxPnL', 'MnPnL', 'MxD'] # named indexes
df1 = df[max_columns].agg(['max','idxmax']).T
df1.columns = ['max_value', 'max_index']
df1['max_index'] = 'Index: ' + df1['max_index'].astype(int).astype(str)
print (df1)
max_value max_index
LNPT 2.0 Index: 3
SNPT 0.0 Index: 0
NLP 6.0 Index: 3
MxPnL 2.3 Index: 3
MnPnL -0.0 Index: 1
MxD -3.0 Index: 0
max_columns = ['LNPT', 'SNPT', 'NLP', 'MxPnL', 'MnPnL', 'MxD'] # named indexes
df1 = df.set_index('element')[max_columns].agg(['max','idxmax']).T
df1.columns = ['max_value', 'max_index']
df1['max_index'] = 'Index: ' + df1['max_index'].astype(str)
print (df1)
max_value max_index
LNPT 2 Index: [ 2. 2. 60.]
SNPT 0 Index: [ 2. 2. 30.]
NLP 6 Index: [ 2. 2. 60.]
MxPnL 2.3 Index: [ 2. 2. 60.]
MnPnL -0.0 Index: [ 2. 2. 40.]
MxD -3 Index: [ 2. 2. 30.]