I have a dataset containing 4 columns and 1 want to apply a condition on 4 columns in on line like IF df.ColumnA > 120 and df,columnB <130 and df.column 4 >80 and print message if condition meet . But when i do this its not working.
Please tell how can I read columns from a file and apply condition on it .
df = pd.read_csv(r"C:\Users\DELL\OneDrive\Desktop\Book1.csv",
names=[ 'SBP', 'DBP', 'HEARTRATE' ])
Time_sensitive_df = (df [ (df [ 'SBP' ] > 110) & (df [ 'DBP' ] > 80) & (df [ 'HEARTRATE' ] < 100) ])
print('\n Time Sensitive Data :\n', Time_sensitive_df)
I want to do this in a if else condition.
CodePudding user response:
Please see revised code below based off your comment and let me know if this is what you need.
import pandas as pd, numpy as np
# sample df
d = {'SBP': [111, 100], 'DBP': [81, 40], 'HEARTRATE':[99,50]}
df = pd.DataFrame(data=d)
df
# get index position based off column name
SBP = df.columns.get_loc('SBP')
DBP = df.columns.get_loc('DBP')
HEARTRATE = df.columns.get_loc('HEARTRATE')
for x in range(len(df)):
if (df.iloc[x,SBP] > 100) & (df.iloc[x,DBP] > 80) & (df.iloc[x,HEARTRATE] < 100):
print('Alert')
else:
print('Normal')
CodePudding user response:
import pandas as pd, numpy as np
# filter results by showing records that meet condition
# sample df
d = {'SBP': [111, 100], 'DBP': [81, 40], 'HEARTRATE':[1,50]}
df = pd.DataFrame(data=d)
df
# if an alert is found, prints alert, else normal
if len(df[(df[ 'SBP' ] > 110) & (df[ 'DBP' ] > 80) & (df[ 'HEARTRATE' ] < 100)]) >0:
print('Alert')
else:
print('Normal')