Pandas and list compare
import numpy as np
import pandas as pddf = pd.DataFrame({'List1':[1,2,3,
4,5,5,11,4],'List 2':[3,5,6,8,9,3,4,9]}, columns=['List1', 'List 2'])
#df.to_excel("list1.xlsx", header=True, index=False)df['Intersect']=pd.DataFrame( np.intersect1d(df['List1'], df['List 2'])) #unique common in both
df['commonin1']=df['List1'][ np.in1d(df['List1'], df['List 2'])] #non unique common items of list 1
df['commonin2']=df['List 2'][np.in1d(df['List 2'], df['List1'])] #non unique common items of list 2
df['1not2']=pd.DataFrame(np. setdiff1d(df['List1'], df['List 2'])) #in list1 but not in list 2
df['2not1']=pd.DataFrame(np. setdiff1d(df['List 2'], df['List1'])) #in list 2 but not in list1
df['1not2NU']=df['List1'][~np. in1d(df['List1'], df['List 2'])] #in list1 but not in list 2 non unique
df['2not1NU']=df['List 2'][~np.in1d(df['List 2'], df['List1'])] #in list 2 but not in list1 non unique
df['exclusive']=pd.DataFrame( np.setxor1d(df['List1'], df['List 2'])) # in a and not b + in b but not a
df=pd.concat([df,pd.DataFrame( np.union1d(df['List1'], df['List 2']), columns=['Union'])], axis=1) # unique all
df
import pandas as pddf = pd.DataFrame({'List1':[1,2,3,
#df.to_excel("list1.xlsx", header=True, index=False)df['Intersect']=pd.DataFrame(
df['commonin1']=df['List1'][
df['commonin2']=df['List 2'][np.in1d(df['List 2'], df['List1'])] #non unique common items of list 2
df['1not2']=pd.DataFrame(np.
df['2not1']=pd.DataFrame(np.
df['1not2NU']=df['List1'][~np.
df['2not1NU']=df['List 2'][~np.in1d(df['List 2'], df['List1'])] #in list 2 but not in list1 non unique
df['exclusive']=pd.DataFrame(
df=pd.concat([df,pd.DataFrame(
df
Comments
Post a Comment