I have a dataframe that has more fields/columns than I want, so I am trying to create a sub dataframe that only has certain fields/columns. The following throws a

TypeError: '(['uniqueid'], ['PARCEL_ID'], ['LAND_USE_T'], ['PROP_IND_T'],['Bldg_Sq_Ft'], ['GEOID'], ['CensusPop'], ['HU_Pop'])' is an invalid key

feat_df = gpd.read_file(state_gdb, layer=join_feat)
new_df = feat_df[['uniqueid'], ['PARCEL_ID'], ['LAND_USE_T'], ['PROP_IND_T'], ['Bldg_Sq_Ft'], ['GEOID'], ['CensusPop'], ['HU_Pop']]

What is the easiest way to create a sub dataframe with only the specific columns I want?


Try to remove the square brackets between the column names, in regular pandas that is what is done for making a dataset subset.

new_df = feat_df[['uniqueid','PARCEL_ID', 'LAND_USE_T', 'PROP_IND_T', 'Bldg_Sq_Ft', 'GEOID', 'CensusPop', 'HU_Pop']]
  • 1
    Dont forget geometry column or new_df will be a pandas dataframe and not a geodataframe – BERA Jun 24 '19 at 18:51

You pass one list with the column names you wish to keep:

new_df = feat_df[['uniqueid', 'PARCEL_ID', 'LAND_USE_T', 'PROP_IND_T', 'Bldg_Sq_Ft', 'GEOID', 'CensusPop', 'HU_Pop']]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.