0

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. 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. However it doesnt go through the whole array values as intended

input.csv file:

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:

my_data = pd.read_csv('input.csv').to_numpy()
max_columns= np.array([1,2,3,7,8,10])
min_columns = np.array([4,5,6,9])

def max_vals():
    results = np.max(my_data[:,max_columns])
    index = np.argmax(results)
    return results, index
    
def min_vals():
    results = np.min(my_data[:,min_columns])
    index = np.argmin(results)
    return results, index

max_values, max_index= max_vals()
min_values, min_index= min_vals()

1 Answer 1

2

Since you already have data frame in pandas , let us try pandas way

df = pd.read_csv('input.csv')
max_columns= np.array([1,2,3,7,8,10])
min_columns = np.array([4,5,6,9])

# max 
index = df[max_columns].idxmax()
values = df[max_columns].max()

# min 
index = df[min_columns].idxmin()
values = df[min_columns].min()
Sign up to request clarification or add additional context in comments.

1 Comment

It is giving me an error. KeyError: "None of [Int64Index([1, 2, 3, 7, 8, 10], dtype='int64')] are in the [columns]"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.