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

for loop inside a def function not working

for i in df.columns:
    if df[i].dtype=='object' and df[i].nunique()>20:
        df=df.drop(columns=[i],axis=1)

The above for loop works as expected, but when i put this under a def function it doesn’t work,
for example- data= Churn_Modelling, the above for loop was able to drop "Surname" but the below def function drop_object_nunique(df) doesn’t removes "Surname"

def drop_object_nunique(data):
    for i in data.columns:
        if data[i].dtype=='object' and data[i].nunique()>20:
            data=data.drop(columns=[i],axis=1)

>Solution :

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

If I understand you correctly, it is a workaround of "Python always does pass-by-value" and the scope of your data frame.

Refer to this answer: https://stackoverflow.com/a/38925257/15017770

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