Home > Mobile >  How to select number columns in pandas dataframe
How to select number columns in pandas dataframe

Time:09-27

How can I select number columns in the below column names

output_df.columns =  Index(['EVENT_ID', 'Date', 'Time', 'Track', '#', 'Distance', 'Betfair Grade','Runners', 'Win Trap', 'Win BSP', '1', '2', '3', '4', '5', '6', '7',
       '8', '9', '10', 'Trap1 Odds Band', 'Trap2 Odds Band', 'Trap3 Odds Band'],
      dtype='object')

I tried this function and I got the below output.

output_df.filter(regex="\d ", axis=1).columns

Index(['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'Trap1 Odds Band',
       'Trap2 Odds Band', 'Trap3 Odds Band'],dtype='object')

I just want the number columns:

['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']
     

CodePudding user response:

new_df = df[df.columns.isnumeric()]

This should work?

CodePudding user response:

Try filtering a full match:

output_df.filter(regex="^\d $", axis=1).columns

Or better without filter:

df.columns[df.columns.isdigit()]
  • Related