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 make this python code more efficient?

I realize this is an incredibly inefficient way to code this, so I’m hoping someone will have suggestions on a more efficient method.

Essentially I’m trying to create a column ("freq") with values of 0 for NA and "Nothing" objects and 1 otherwise. Sample df:

i   obj           freq

0.  Nothing        0
1.  Something      1
2.  NaN            0
3.  Something      1


for i in range(0,len(df)):
  if str(df["obj"].iloc[i]) == "Nothing" or str(df["obj"].iloc[i]) == NaN:
    d["freq"].iloc[i] = 0
  else:
    df["freq"].iloc[i] = 1

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

>Solution :

You can use np.where()

import pandas as pd 
import numpy as np

df = pd.DataFrame({'obj': {0: 'Nothing', 1: 'Something', 2: np.nan, 3: 'Something'}})

df['freq'] = np.where((df['obj'] == 'Nothing') | (df['obj'].isnull()), 0, 1)
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