简体   繁体   中英

Python Extract matching strings from a list

My dataframe has many columns. I want to extract columns starting with 9. Code:

df_columns = Index(['_id', 'Time', '2.1', '2.2', '2.3', '2.4', '2.5', '2.6', '2.7', '2.8',
       '2.9', '2.10', '2.11', '2.12', '2.13', '2.14', '2.15', '2.16', '2.17',
       '2.18', '2.19', '2.20', '9.1', '9.2', '9.3', '9.4', '9.5', '9.6', '9.7',
       '9.8', '9.9', '9.10', '9.11', '9.12', '9.13', '9.14', '9.15', '9.16',
       '9.17', '9.18', '9.19', '9.20'],
      dtype='object')
col9s = df_columns.str.findall(r'\b9.')

Present solution:

cols = 
Index([    [],     [],     [],     [],     [],     [],     [],     [],     [],
           [],     [],     [],     [],     [],     [],     [],     [],     [],
           [],     [],     [],     [], ['9.'], ['9.'], ['9.'], ['9.'], ['9.'],
       ['9.'], ['9.'], ['9.'], ['9.'], ['9.'], ['9.'], ['9.'], ['9.'], ['9.'],
       ['9.'], ['9.'], ['9.'], ['9.'], ['9.'], ['9.']],
      dtype='object')

Expected answer:

col9s = ['9.1', '9.2', '9.3', '9.4', '9.5', '9.6', '9.7',
       '9.8', '9.9', '9.10', '9.11', '9.12', '9.13', '9.14', '9.15', '9.16',
       '9.17', '9.18', '9.19', '9.20']

Use filter with a regex:

col9s = df.filter(regex=r'^9\.').columns

Or, to directly subset the columns:

df2 = df.filter(regex=r'^9\.')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM