[英]Printing the max and min values with pandas Python
I am trying to write a function that prints out all the max and min values and their indexes for the specified columns in the input.csv
file.我正在尝试编写一个 function 打印出
input.csv
文件中指定列的所有最大值和最小值及其索引。 The columns I want to return the max values of are referenced in the max_columns
variable and the one to return min values are in the min_columns
variable.我要返回最大值的列在
max_columns
变量中引用,而要返回最小值的列在min_columns
变量中。 However it doesn't go through the whole array values as intended I tried to do it with pandas howver it does not work.但是,它没有按预期通过整个数组值 go 我尝试使用 pandas 来做到这一点,但是它不起作用。 The code down below was achieved from my previous post:
下面的代码是从我之前的帖子中实现的:
input.csv file:输入.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
Code:代码:
df = pd.read_csv('STDOutputs_Q1.csv')
max_columns= np.array([1,2,3,7,8,10])
min_columns = np.array([4,5,6,9])
def max_vals():
max_index = df[max_columns].idxmax()
max_values = df[max_columns].max()
return results, index
def min_vals():
min_index = df[min_columns].idxmin()
min_values = df[min_columns].min()
return results, index
max_values, max_index= max_vals()
min_values, min_index= min_vals()
max_columns
and min_columns
hold integer indexes, so you should iloc
them as df.iloc[:, max_columns]
and df.iloc[:, min_columns]
: max_columns
和min_columns
保存 integer 索引,因此您应该将它们iloc
为df.iloc[:, max_columns]
和df.iloc[:, min_columns]
:
max_columns = np.array([1, 2, 3, 7, 8, 10]) # numeric indexes
min_columns = np.array([4, 5, 6, 9])
def max_vals():
max_index = df.iloc[:, max_columns].idxmax() # so not df[max_columns]
max_values = df.iloc[:, max_columns].max()
return max_values, max_index
def min_vals():
min_index = df.iloc[:, min_columns].idxmin()
min_values = df.iloc[:, min_columns].min()
return min_values, min_index
Alternatively you could define max_columns
and min_columns
by name, in which case df[max_columns]
and df[min_columns]
would work:或者,您可以按名称定义
max_columns
和min_columns
,在这种情况下df[max_columns]
和df[min_columns]
可以工作:
max_columns = ['LNPT', 'SNPT', 'NLP', 'MxPnL', 'MnPnL', 'MxD'] # named indexes
min_columns = ['NSP', 'TNT', 'TPnL', 'MxU']
def max_vals():
max_index = df[max_columns].idxmax() # so not df.iloc[:, max_columns]
max_values = df[max_columns].max()
return max_values, max_index
def min_vals():
min_index = df[min_columns].idxmin()
min_values = df[min_columns].min()
return min_values, min_index
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.