0

I am trying to format the data in input.csv so that it returns the indexes that satisfies the conditions of Indexes. I want the code to print out all the indexes of rows that have all the values defined in the #intervals. Only the first row of element [ 2. 2. 30.] satisfies for the limits defined at the Indexes variables. Essentially I want to print out all the rows: Thats satisfy the condition: if column['MxU'] >= MxU and column['SNPT'] >= SNPT..... and column['MxD'] >= MxD

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:

df = pd.read_csv('input.csv')

#intervals
MxU= 17100
SNPT= 1
NLP= 3
MnPnL= -0.1
MxD= 0

#variables used for formatting
Indexes = [MxU,SNPT,NLP,MnPnL,MxD]
#all columns in csv listed
columns = ['LNPT', 'SNPT', 'NLP', 'MxPnL', 'NSP', 'TNT', 'TPnL', 'MnPnL','MxU','MxD']

def intersect() :#function for 
    for i in columns:
        if str(Indexes[i]) in columns[i]:
            for k in Indexes:
                formating = df[columns] >= Indexes[k]
        
    
intersect() #calling function

Expected Output:

row: 1 element:[ 2.  2. 30.]
tony selcuk
  • 709
  • 3
  • 11

1 Answers1

1

If I get your point, you can store your column interval in a dictionary. Then loop through the columns you want to check to compare with the interval dictionary.

You can use np.logical_and and reduce to simplify the loop.

import numpy as np

intervals = {
    'MxU': 17100,
    'SNPT': 1,
    'NLP': 3,
    'MnPnL': -0.1,
    'MxD': 0
}

columns = ['NLP', 'MnPnL']

mask = np.logical_and.reduce([df[col] >= intervals[col] for col in columns])

Then use boolean indexing to select the desired rows

df_ = df.loc[mask, 'element']
# print(df_)

0    [ 2.  2. 30.]
4    [ 2.  2. 70.]
Name: element, dtype: object
Ynjxsjmh
  • 28,441
  • 6
  • 34
  • 52