Home > Mobile >  Trying to filter columns using the wildcard *
Trying to filter columns using the wildcard *

Time:03-29

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]
  • Related