Calculus
Calculus

Reputation: 781

Pandas how to drop a level and column

How do I drop a level on the columns and also remove all columns except "City" and "geometry"

             AREA  AA5000  HOLD  LEVEL  TYPE   ID  TAZCE10_N                                           geometry
City                                                                                                                    
Detroit  0.120861     2.0     1.0       5.0       0.0  1.0     1101.0  (POLYGON ((-83.47904699999999 41.706103, -83.4...
Austin        NaN     NaN     NaN       NaN       NaN  NaN        NaN  (POLYGON ((-97.577304 29.610979, -97.580744999...

so that it looks like this:

City      geometry                                                                                                                
Detroit  (POLYGON ((-83.47904699999999 41.706103, -83.4...
Austin   (POLYGON ((-97.577304 29.610979, -97.580744999...

Technically this is a geopandas df that I temporarily convert to normal df because geopandas doesn't have droplevel(). I've tried the statements below but they don't work and droplevel throws an error "AttributeError: 'Index' object has no attribute 'droplevel'.

    df.columns = [c[-1] for c in df.columns]
    df.reset_index()
    df.columns.droplevel()

Upvotes: 0

Views: 557

Answers (1)

BENY
BENY

Reputation: 323366

You can try dropna with thresh

df=df.dropna(axis=1,thresh=len(df)).reset_index()

Upvotes: 1

Related Questions