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



我有两个独立的panda数据帧,其中一个跟踪火车站的站台是否空闲,另一个跟踪列车的运行(注意,我只是处于概念验证阶段,我很感激我的代码不整洁(。

代码如下:

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'] == TrainNumber, 'To'].iloc[0]
if(stations_control.loc[stations_control['Location']==b]['BlockTaken'] == 0):

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

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

最新更新