pandas-python中的If语句
我有这样一个数据帧:pandas-python中的If语句,python,python-3.x,pandas,dataframe,if-statement,Python,Python 3.x,Pandas,Dataframe,If Statement,我有这样一个数据帧: 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
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
我想知道Category=Teacher在工作日是否可用,它应该在输出中显示TechID 003006&007
我试过这个密码
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
但这只显示TechID 007行。某些TechID可能在周末和工作日都可用。这也应予以考虑。
有人能帮我吗?当答案为0时,请求应忽略筛选器,而不是请求0值。一种简单的方法是对0使用
isin([0,1])
,对1使用isin([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))]
当答案为0时,请求应忽略筛选器,而不是请求0值。一种简单的方法是对0使用
isin([0,1])
,对1使用isin([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))]
首先,你的答案是004、006和007 一个简单的方法是:
df1_new[(df1_new['Category']='Teacher')和(df1_new['Weekdays']==1)]
首先,你的答案是004、006和007
一个简单的方法是:
df1_new[(df1_new['Category']='Teacher')和(df1_new['Weekdays']==1)]
尝试以下方法:
工作日可供选择的教师:
df2 = df.loc[(df['Category'] == 'Teacher') & (df['Weekdays'] == 1)]
df2
df2 = df.loc[(df['Category'] == 'Teacher') & (df['Weekdays'] & df['LateNight']==1)]
df2
输出:
TechId Rating Category LateNight Weekend Weekdays
004 1 Teacher 1 0 1
006 0 Teacher 1 1 1
007 2 Teacher 0 0 1
TechId Rating Category LateNight Weekend Weekdays
004 1 Teacher 1 0 1
006 0 Teacher 1 1 1
教师在工作日和深夜可用:
df2 = df.loc[(df['Category'] == 'Teacher') & (df['Weekdays'] == 1)]
df2
df2 = df.loc[(df['Category'] == 'Teacher') & (df['Weekdays'] & df['LateNight']==1)]
df2
输出:
TechId Rating Category LateNight Weekend Weekdays
004 1 Teacher 1 0 1
006 0 Teacher 1 1 1
007 2 Teacher 0 0 1
TechId Rating Category LateNight Weekend Weekdays
004 1 Teacher 1 0 1
006 0 Teacher 1 1 1
试试这个:
工作日可供选择的教师:
df2 = df.loc[(df['Category'] == 'Teacher') & (df['Weekdays'] == 1)]
df2
df2 = df.loc[(df['Category'] == 'Teacher') & (df['Weekdays'] & df['LateNight']==1)]
df2
输出:
TechId Rating Category LateNight Weekend Weekdays
004 1 Teacher 1 0 1
006 0 Teacher 1 1 1
007 2 Teacher 0 0 1
TechId Rating Category LateNight Weekend Weekdays
004 1 Teacher 1 0 1
006 0 Teacher 1 1 1
教师在工作日和深夜可用:
df2 = df.loc[(df['Category'] == 'Teacher') & (df['Weekdays'] == 1)]
df2
df2 = df.loc[(df['Category'] == 'Teacher') & (df['Weekdays'] & df['LateNight']==1)]
df2
输出:
TechId Rating Category LateNight Weekend Weekdays
004 1 Teacher 1 0 1
006 0 Teacher 1 1 1
007 2 Teacher 0 0 1
TechId Rating Category LateNight Weekend Weekdays
004 1 Teacher 1 0 1
006 0 Teacher 1 1 1
我希望技术人员在深夜和周末或深夜和周末或仅在工作日提供服务。根据要求。我希望技术人员在深夜和周末或深夜和周末或仅在工作日提供服务。根据需要。