Df .shape not adjusting after dropping columns

Question:

I’m extremely new to Python and Jupyter Notebooks so bear with me, but I haven’t been able to solve this issue after some deep diving.

After using .drop to remove several columns, I am not seeing the change reflected in .shape.

result after using drop, but the shape output not reflecting

The df starts as 1404 x 68, after executing .drop the output says 1404 x 38, but then when I run .shape it jumps back to 1404 x 68.

df_realestate.drop(['Zoning Class', 'Lot Shape', 'Lot Config', 'Land Slope', 'Bldg Type', 
                                   'House Style', 'Roof Style', 'Roof Material', 'Exterior Primary', 'Masonry/Veneer', 'Exterior Qual', 'Exterior Cond',
                                   'Foundation', 'Basement Height', 'Basement Cond', 'Basement Exposure', 'Basement Finish', 'Heating Qual', 'CentralAir', 
                                   'Electrical', 'Functionality', 'Fireplce Qual', 'Garage Type', 'Garage Qual', 'Garage Cond', 'Paved Drive',
                                   'Pool Qual', 'Fence', 'Sale Type', 'Year Remod Add'], axis = 1)

df_realestate.shape

Thanks for your help! I’m sure it is something simple I am missing.

Asked By: Kathryn Moore

||

Answers:

Yes. Like most pandas APIs, the drop method does not do its operation in place. Instead, it RETURNS a modified DataFrame, which you are discarding.

You can add inplace=True as a parameter, or you can do

df_realestate = df_realestate.drop(...)
Answered By: Tim Roberts
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.