Skip to content Skip to sidebar Skip to footer

Formatting Rows That Satisfy Conditions Pandas Python

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

Solution 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

Post a Comment for "Formatting Rows That Satisfy Conditions Pandas Python"