Python 检查两个不同数据帧字段的值是否相等

Python 检查两个不同数据帧字段的值是否相等,python,pandas,Python,Pandas,我有两个独立的熊猫数据帧,其中一个跟踪火车站站台是否空闲,另一个跟踪火车的移动(注意,我只是在概念验证阶段,我知道我的代码不整洁) 代码如下: L = [] M = [] x = 0 for i in range(0,k*2): L.append(0) M.append(x) if (i == k): x = 1 list_of_tuples = list(zip(M, L)) blocks_df = pd.DataFrame(list_of_tuples

我有两个独立的熊猫数据帧,其中一个跟踪火车站站台是否空闲,另一个跟踪火车的移动(注意,我只是在概念验证阶段,我知道我的代码不整洁)

代码如下:

L = []
M = []
x = 0
for i in range(0,k*2): 
   L.append(0)
   M.append(x)
   if (i == k):
       x = 1

list_of_tuples = list(zip(M, L))  
blocks_df = pd.DataFrame(list_of_tuples, columns = ['Direction', 'BlockTaken'])  

L = ["London Depot", "London Platform", "Birmingham Platform", "Crossover"]
M = [0,0,0,0]
list_of_tuples = list(zip(L, M))  

stations_control = pd.DataFrame(list_of_tuples, columns = ['Location', 'BlockTaken'])

for i in range (0,3600): 
    if (i%300==0): #Every 5 minutes, a new train enters service
        print("Train " + str(TrainNumber) + " leaving depot for " + str(train_df.loc[train_df['Train_Number'] == TrainNumber, 'Start_Station'].iloc[0]) + " at " + str(t.time()) )
        
        train_df.loc[train_df['Train_Number'] == TrainNumber, 'Dep'] = 'N'
        train_df.loc[train_df['Train_Number'] == TrainNumber, 'Dep_Time'] = t.time()
        train_df.loc[train_df['Train_Number'] == TrainNumber, 'From'] = L[0]
        train_df.loc[train_df['Train_Number'] == TrainNumber, 'To'] = L[1]
        
        if(stations_control[train_df.loc[train_df['Train_Number'] == TrainNumber, 'To']]['BlockTaken'] ==0):  
            print("Platform is free!!!")
        t = t + datetime.timedelta(0,300)
        #TrainNumber+=1
        
我知道我把if语句从结尾的4行做错了,但我不知道如何正确地做。我要检查的是火车要去哪里,如果站台是空的,打印出来。这里的正确语法是什么?

我想是的

b=train_-df.loc[train_-df['train_-Number']==train-Number,'To'].iloc[0]
如果(站点控制.loc[站点控制['Location']==b]['blocktake']==0):

在这里回答我自己的问题。谢谢你们的帮助

if(stations_control.loc[stations_control['Location']==b].BlockTaken.iloc[0]== 0):
            print("Platform is free!!!")

如果站点_control.loc[L[1],'blocktake']=0:
?…引发了什么异常?在if和开括号之间需要空格吗?嗨,Richie,谢谢你的帮助。这给了我一个错误:KeyError:“伦敦平台”嗨,谢谢你的帮助。我得到一个错误“序列的真值不明确。使用a.empty、a.bool()、a.item()、a.any()或a.all()。”
stations\u control.loc[stations\u control['Location']==b]['blocktake'].all()==0
或`stations\u control.loc[stations\u control['Location']==b][blocktake']==0``您能给我看一下python stations\u control.loc[stations\u control['Location']==b]['BlockTake'].名称:BlockTake,数据类型:Int64以及train\u df.loc[train\u df['train\u Number']==train Number'To']的输出结果吗?