How Can Make Pandas Columns Compare Check Cell?
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 CX
Solution 1:
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 columnsdf = 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 status0 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
Post a Comment for "How Can Make Pandas Columns Compare Check Cell?"