1

I manually select the columns in a pandas dataframe using

 df_final = df[['column1','column2'.......'column90']]

Instead I provide the list of column names in a list by

 dp_col =  [col for col in df if col.startswith('column')]

But not sure how to use this list to get only those set of columns from the source dataframe.

Georgy
  • 12,464
  • 7
  • 65
  • 73
hydesingh
  • 47
  • 4

2 Answers2

1

You can use this as the list of columns to select, so:

df_final = df[[col for col in df if col.startswith('column')]]

The "origin" of the list of strings is of no importance, as long as you pass a list of strings to the subscript, this will normally work.

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
0

Use loc access with boolean masking:

df.loc[:, df.columns.str.startswith('column')]
Quang Hoang
  • 146,074
  • 10
  • 56
  • 74