Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I replace is.null values with condition using pandas/numpy?

My dataset:

data={'A':['1210226','1210226','1210226','1210302','1210336','1210336','1210336'],'B':['','','2@.com','1@.com','','','1@.com']}
df=pd.DataFrame(data)
print(df)

I filter the column to blanks in excel == python( used is.null)

Data set in excel

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

1

The condition I want to apply in pandas is =IF(A1=A3,B3,"") formula is written in excel ( please review the image), and sheet is filtered with blanks. I know O have to use is.null() then i want to apply condition to the is.null in order to get required output.

2

Final output should be something like below image

3

>Solution :

You can try the following:

data={'A':['1210226','1210226','1210226','1210302','1210336','1210336','1210336'],'B':['','','2@.com','1@.com','','','1@.com']}
df=pd.DataFrame(data)

df.drop("B", axis=1).merge(df.groupby("A").max(), on="A")

Output:

    A            B
0   1210226     2@.com
1   1210226     2@.com
2   1210226     2@.com
3   1210302     1@.com
4   1210336     1@.com
5   1210336     1@.com
6   1210336     1@.com
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading