import pandas as pd
df1=pd.DataFrame({'authorID':['12','34','56']})
df2=pd.DataFrame({'authorID':['12','56','78','97']})
original_users=set(df1[['authorID']].values.reshape(-1))
for i in df2[['authorID']].values.reshape(-1):
if i not in original_users:
print(i)
>>> old = {'451223', '120346', '122355', '787456'}
>>> new = {'451223', '025660', '122355', '022000'}
>>> {x for x in new if x not in old}
{'025660', '022000'}
你可以这样做:
那么
^{pr2}$df1
是并且
df2
是输出是
注意,
df1[['authorID']].values.reshape(-1)
使您能够将列转换为可以迭代的列表。对于这个例子,df1[['authorID']].values.reshape(-1)
是array(['12', '34', '56'], dtype=object)
。将这个数组转换为set
是为了提高复杂性。在另一种比较
authorID
s的方法:如果两个CSV文件都加载为iterables(
f.readlines()
?)然后,您可以简单地使用集合差分返回day2.csv元素的新集合,这些元素不在day1.csv中:或者
^{pr2}$相关问题 更多 >
编程相关推荐