как отбросить ряд столбцов панд по нескольким ключевым словам - PullRequest
0 голосов
/ 04 сентября 2018

Я получил кадр данных панд. Он полон ненужных функций, которые я хотел бы удалить. Прямо сейчас я делаю следующее, что грязно Как я мог получить это более питоническим способом?

 features_to_include= mydf.columns.tolist()
 features_to_include=[f for f in features_to_include if 'stopword1' not in f]

 features_to_include=[f for f in features_to_include if 'stopwordN' not in f]

[... другие 90 из них]

    features_to_include=[f for f in features_to_include if 'password1' in f]
    features_to_include=[f for f in features_to_include if 'passwordN' in f]

[... другие 90 из них]

РЕДАКТИРОВАТЬ: «stopword1» и «password1» имеют значение , а не in X.columns пример имени X.columns может быть: feature99_stopword1

Ответы [ 2 ]

0 голосов
/ 04 сентября 2018

Вы можете попробовать использовать filter:

df.filter(regex='password|stopword1', axis=1)

Или, если у нас есть список:

cols = ['password','passwordN','stopword1','stopwordN']
mydf.filter(regex='|'.join(cols), axis=1)
0 голосов
/ 04 сентября 2018

Я думаю, что нужно str.contains:

L = ['stopword1','stopwordN','password1', 'passwordN']
#thanks roganjosh for suggestion
L = set(['stopword1','stopwordN','password1', 'passwordN'])

mydf = mydf.loc[:, mydf.columns.str.contains('|'.join(L))]

Sample

mydf = pd.DataFrame({'feature99_stopword1':list('abcdef'),
                   'feature99_stopword':[4,5,4,5,5,4],
                   'C':[7,8,9,4,2,3],
                   'd_stopword1':[1,3,5,7,1,0],
                   'password1':[5,3,6,9,2,4],
                   'F':list('aaabbb')})
print (mydf)
  feature99_stopword1  feature99_stopword  C  d_stopword1  password1  F
0                   a                   4  7            1          5  a
1                   b                   5  8            3          3  a
2                   c                   4  9            5          6  a
3                   d                   5  4            7          9  b
4                   e                   5  2            1          2  b
5                   f                   4  3            0          4  b

L = ['stopword1','stopwordN','password1', 'passwordN']
mydf = mydf.loc[:, mydf.columns.str.contains('|'.join(L))]
print (mydf)
  feature99_stopword1  d_stopword1  password1
0                   a            1          5
1                   b            3          3
2                   c            5          6
3                   d            7          9
4                   e            1          2
5                   f            0          4
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...