I have a two file.
a.txt has the below data.
Zone,Aliase1,Aliase2
VNX7600SPB3_8B3_H1,VNX7600SPB3,8B3_H1
VNX7600SPBA_8B4_H1,VNX7600SPA3,8B4_H1
CX480SPA1_11B3_H1,CX480SPA1,11B3_H1
CX480SPB1_11B4_H1,CX480SPB1,11B4_H1
b.txt has the below data.
Zone,Aliase1,Aliase2
VNX7600SPB3_8B3_H1,VNX7600SPB3,8B3_H1
CX480SPA1_11B3_H1,CX480SPA1,11B3_H1
I want made result about compare two files zone columns like below.
Zone,Aliase1,Aliase2,Status
VNX7600SPB3_8B3_H1,VNX7600SPB3,8B3_H1,Active
VNX7600SPBA_8B4_H1,VNX7600SPA3,8B4_H1,Not used
CX480SPA1_11B3_H1,CX480SPA1,11B3_H1,Active
CX480SPB1_11B4_H1,CX480SPB1,11B4_H1,Not used
How can I make result.
I tried using pandas. But I can't make result.
please help me.
I think you need merge
with outer join and parameter indicator=True
and then rename
column name and map
3 possible values (both
, left_only
and right_only
):
#if no 'on' parameter, merge all columns
df = pd.merge(df1, df2, how='outer', indicator=True)
df = df.rename(columns={'_merge':'status'})
d = {'left_only':'Not used', 'both':'Active', 'right_only':'b_file_only'}
df['status'] = df['status'].map(d)
print (df)Zone Aliase1 Aliase2 status
0 VNX7600SPB3_8B3_H1 VNX7600SPB3 8B3_H1 Active
1 VNX7600SPBA_8B4_H1 VNX7600SPA3 8B4_H1 Not used
2 CX480SPA1_11B3_H1 CX480SPA1 11B3_H1 Active
3 CX480SPB1_11B4_H1 CX480SPB1 11B4_H1 Not used
If you want compare only by Zone
column add parameter on
and filter in df2
column by subset ([[]]
):
df = pd.merge(df1, df2[['Zone']], how='outer', indicator=True, on='Zone')
df = df.rename(columns={'_merge':'status'})
d = {'left_only':'Not used', 'both':'Active', 'right_only':'b_file_only'}
df['status'] = df['status'].map(d)
print (df)Zone Aliase1 Aliase2 status
0 VNX7600SPB3_8B3_H1 VNX7600SPB3 8B3_H1 Active
1 VNX7600SPBA_8B4_H1 VNX7600SPA3 8B4_H1 Not used
2 CX480SPA1_11B3_H1 CX480SPA1 11B3_H1 Active
3 CX480SPB1_11B4_H1 CX480SPB1 11B4_H1 Not used