Search code examples
pythonfor-loopdrop

Type Error : NoneType is not subscriptable


for i in Leads.columns:
    if (Leads[i].isnull().sum()/Leads.shape[0]) > 0.40:
        Leads = Leads.drop(i,axis=1,inplace = True)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_17092/1952428833.py in <module>
      2 
      3 for i in Leads.columns:
----> 4     if (Leads[i].isnull().sum()/Leads.shape[0]) > 0.40:
      5         Leads = Leads.drop(i,axis=1,inplace = True)

TypeError: 'NoneType' object is not subscriptable
for i in Leads.columns:
    if (Leads[i].isnull().sum()/Leads.shape[0]) > 0.40:
        Leads = Leads.drop(i,axis=1,inplace = True)

I was expecting the columns having more than 40% missing values to be dropped


Solution

  • inplace parameter makes the modificaiton on the called object, and returns nothing

    Either remove the assignment

    for i in Leads.columns:
        if (Leads[i].isnull().sum() / Leads.shape[0]) > 0.40:
            Leads.drop(i, axis=1, inplace=True)
    

    Or remove the inplace

    for i in Leads.columns:
        if (Leads[i].isnull().sum() / Leads.shape[0]) > 0.40:
            Leads = Leads.drop(i, axis=1)