Reputation:
I have a dataframe like this:
TechId Rating Category LateNight Weekend Weekdays
001 5 Teacher 1 1 0
002 4 Student 0 1 1
003 3 Driver 1 0 1
004 1 Teacher 1 0 1
005 1 Student 1 1 0
006 0 Teacher 1 1 1
007 2 Teacher 0 0 1
I wanted to find if the Category = Teacher is available on Weekdays, that should display TechID 003,006 & 007 in the output.
I tried this code,
Category = int(input("Enter the Trade ID you want technicians in:"))
Latenight = int(input("Press 1 if you want technicians in Latenight/else press 0:"))
Weekends = int(input("Press 1 if you want technicians in Weekends/else press 0:"))
WeekDays = int(input("Press 1 if you want technicians in WeekDays/else press 0:"))
df2 = df1_new[(df1_new['Category'] == Category) &(df1_new['Weekends'] == Weekends) & (df1_new['LateNight'] == Latenight)]
df2 = df1_new[(df1_new['Category'] == Category) &(df1_new['WeekDays'] == WeekDays) & (df1_new['LateNight'] == Latenight)]
df2 = df1_new[(df1_new['Category'] == Category) &(df1_new['WeekDays'] == WeekDays) & (df1_new['LateNight'] == Latenight) & (df1_new['Weekends'] == Weekends)]
df2_new= df2.sort_values(by=['divcount'], ascending=False)
df2_new.head()
Enter the Category you want : Teacher
Press 1 if you want technicians in Latenight/else press 0:0
Press 1 if you want technicians in Weekends/else press 0:0
Press 1 if you want technicians in WeekDays/else press 0:1
But this displays only TechID 007 row. Some TechID might be available on both Weekend & weekdays. That should also be considered. Can anyone help me out?
Upvotes: 0
Views: 58
Reputation: 821
Try this:
Teachers available on Weekdays:
df2 = df.loc[(df['Category'] == 'Teacher') & (df['Weekdays'] == 1)]
df2
Output:
TechId Rating Category LateNight Weekend Weekdays
004 1 Teacher 1 0 1
006 0 Teacher 1 1 1
007 2 Teacher 0 0 1
Teachers available on Weekdays and LateNight:
df2 = df.loc[(df['Category'] == 'Teacher') & (df['Weekdays'] & df['LateNight']==1)]
df2
Output:
TechId Rating Category LateNight Weekend Weekdays
004 1 Teacher 1 0 1
006 0 Teacher 1 1 1
Upvotes: 0
Reputation: 1729
First of all, your answer will be 004, 006 and 007.
And a short way to do this, will be:
df1_new[(df1_new['Category'] == 'Teacher') & (df1_new['Weekdays'] == 1)]
Upvotes: 0
Reputation: 149185
The request should ignore the filter when the answer is 0 instead of asking for 0 values. A simple way would be to use isin([0,1])
for 0 and isin([1])
for 1:
Category = int(input("Enter the Trade ID you want technicians in:"))
Latenight = [1] if int(input("Press 1 if you want technicians in Latenight/else press 0:")) else [0,1]
Weekends =[1] if int(input("Press 1 if you want technicians in Weekends/else press 0:")) else [0,1]
WeekDays = [1] if int(input("Press 1 if you want technicians in WeekDays/else press 0:")) else [0,1]
df2 = df1_new[(df1_new['Category'] == Category) &(df1_new['WeekDays'].isin(WeekDays)) & (df1_new['LateNight'].isin(Latenight)) & (df1_new['Weekends'].isin(Weekends))]
Upvotes: 2