我正在创建一个python脚本,该脚本确定file_1的列中是否存在file_2的列中不存在的值。如果file_1的某个列中的值不存在,而file_2的某个列中不存在,我希望将这些值打印在输出中。
当前,我正在使用Pandas将值加载到数据帧中,然后使用“ while”循环遍历这些值,但是,这似乎并没有按预期遍历这些值。有想法吗?
import pandas as pd
dataframe_server_permissions = pd.read_csv("server_permissions.csv")
dataframe_database_permissions = pd.read_csv("database_permissions.csv")
number_of_values = len(dataframe_server_permissions)
i = 0
while i < number_of_values:
if dataframe_server_permissions['Permissions'][i] in dataframe_database_permissions['Rights']:
print("The permission on the server #" + dataframe_server_permissions['Permissions'][i] + "# exists in the database")
elif dataframe_server_permissions['Permissions'][i] not in dataframe_database_permissions['Rights']:
print("The permission on the server #" + dataframe_server_permissions['Permissions'][1] + "# DOES NOT exist in the database #")
i+=1
答案 0 :(得分:1)
您可以使用.isin()函数而无需循环
compare=dataframe_database_permissions['Rights'].tolist()
dataframe_server_permissions['flag'] =
dataframe_server_permissions['Permissions'].isin(compare).astype(int)
#now you can check for values which are not in rights by filtering using flag
print (dataframe_server_permissions[dataframe_server_permissions['flag']==0])
答案 1 :(得分:0)
您可以使用isin
方法,该方法可能会产生NaN
行,可以将其删除:
dataframe_server_permissions[~dataframe_database_permissions.isin(dataframe_server_permissions)].dropna(how='all')