I am trying to create a new dataframe object from an existing dataset that contains multiple similar columns that start with "shape_" I am trying to use the wildcard (*) but I am getting an error.
new_df = df[['date', 'lat', 'long', 'shape_*']]
from all the examples I have seen this should work but I am getting the error:
KeyError: "['shape_*'] not in index"
CodePudding user response:
Use filter
:
cols = list(df.filter(like='shape_').columns)
new_df = df[['date', 'lat', 'long'] cols]
Or with a regex:
cols = list(df.filter(regex='^shape_.*').columns)
new_df = df[['date', 'lat', 'long'] cols]