我的資料框有很多列。我想提取以9.
代碼開頭的列:
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.')
目前的解決方案:
cols =
Index([ [], [], [], [], [], [], [], [], [],
[], [], [], [], [], [], [], [], [],
[], [], [], [], ['9.'], ['9.'], ['9.'], ['9.'], ['9.'],
['9.'], ['9.'], ['9.'], ['9.'], ['9.'], ['9.'], ['9.'], ['9.'], ['9.'],
['9.'], ['9.'], ['9.'], ['9.'], ['9.'], ['9.']],
dtype='object')
預期答案:
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']
uj5u.com熱心網友回復:
與正則運算式一起使用filter:
col9s = df.filter(regex=r'^9\.').columns
或者,直接子集列:
df2 = df.filter(regex=r'^9\.')
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/433550.html
