How can make pandas columns compare check cell?

2024/10/13 11:27:23

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.

Answer

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
https://en.xdnf.cn/q/118088.html

Related Q&A

Flask argument of type _RequestGlobals is not iterable

When I tried to use Flask-WTForms, I followed these steps:from flask_wtf import Form from wtforms import StringField, PasswordField from wtforms.validators import DataRequired, Emailclass EmailPassword…

PumpStreamHandler can capture the process output in realtime

I try to capture a python process output via apache-commons-exec. But it looks like it wont print the output, the output is only displayed after I the python process is finished.Heres my java codeComma…

Freezing a CNN tensorflow model into a .pb file

Im currently experimenting with superresolution using CNNs. To serve my model Ill need to frezze it first, into a .pb file, right? Being a newbie I dont really know how to do that. My model basically …

flattening a list or a tuple in python. Not sure what the error is

def flatten(t):list = []for i in t:if(type(i) != list and type(i) != tuple):list.append(i)else:list.extend(flatten(i))return listHere is the function that Ive written to flatten a list or a tuple that …

Counting word occurrences in csv and determine row appearances

I have a csv file such as the following in one column. The symbols and numbers are only to show that the file does not just contain text. I have two objectives:count the number of occurrences of a wo…

Converting Python 3.6 script to .exe? [duplicate]

This question already has answers here:How can I convert a .py to .exe for Python?(8 answers)Closed 6 years ago.I would like to convert a .py file to an .exe. I am using Python 3.6. I already tried py…

Error while reading csv file in python

I tried to run following programme in ubuntu terminal but I am getting some error. But it is not giving any error in jupyter notebookFile "imsl.py", line 5 SyntaxError: Non-ASCII character \x…

Searching for a USB in Python is returning there is no disk in drive

I wrote up a function in Python that looks for a USB drive based on a key identifier file, however when called upon it returns There is no disk in the drive. Please insert a disk into drive D:/ (which …

Counting phrases in Python using NLTK

I am trying to get a phrase count from a text file but so far I am only able to obtain a word count (see below). I need to extend this logic to count the number of times a two-word phrase appears in th…

Break python list into multiple lists, shuffle each lists separately [duplicate]

This question already has answers here:Shuffling a list of objects [duplicate](26 answers)Closed 7 years ago.Lets say I have posts in ordered list according to their date.[<Post: 6>, <Post: 5&…