Замените несколько символов из одного столбца на NaN в Python - PullRequest
1 голос
/ 07 февраля 2020

Я хочу заменить слова позиции из столбца strings: если они присутствуют как единственные или множественные, но объединяются с , и space.

    id                         strings
0    1                           south
1    2                           north
2    3                            east
3    4                            west
4    5               west, east, south
5    6                      west, west
6    7                    north, north
7    8                    north, south
8    9  West Corporation global office
9   10                     West-Riding
10  11      University of West Florida
11  12                       Southwest

Мой ожидаемый результат понравится это. Обратите внимание, если они являются компонентами фразы или слова, тогда мне не нужно их заменять.

Возможно ли это сделать? Спасибо.

    id                         strings
0    1                             NaN
1    2                             NaN
2    3                             NaN
3    4                             NaN
4    5                             NaN
5    6                             NaN
6    7                             NaN
7    8                             NaN
8    9  West Corporation global office
9   10                     West-Riding
10  11      University of West Florida
11  12                       Southwest

Следующий код работает, но мне просто интересно, есть ли еще несколько лаконичных методов?

df['strings'].astype(str).replace('south', np.nan).replace('north', np.nan)\
.replace('west', np.nan).replace('east', np.nan).replace('west, east', np.nan)\
.replace('west, west', np.nan).replace('north, north', np.nan).replace('west, east', np.nan)\
.replace('north, south', np.nan)

Ответы [ 2 ]

2 голосов
/ 07 февраля 2020

Первое использование Series.str.split, прямое заполнение для замены отсутствующих значений, проверьте, соответствуют ли все совпадающие значения DataFrame.isin и DataFrame.all для маски и последний набор пропущенных значений: Series.mask:

L = ['south','north','east','west']
m = df['strings'].str.split(', ', expand=True).ffill(axis=1).isin(L).all(axis=1)

df['strings'] = df['strings'].mask(m)
print (df)
    id                         strings
0    1                             NaN
1    2                             NaN
2    3                             NaN
3    4                             NaN
4    5                             NaN
5    6                             NaN
6    7                             NaN
7    8                             NaN
8    9  West Corporation global office
9   10                     West-Riding
10  11      University of West Florida
11  12                       Southwest

Другая идея с set s, isdisjoint и Series.where:

m = [set(x.split(', ')).isdisjoint(L) for x in df['strings']]
df['strings'] = df['strings'].where(m)
print (df)
    id                         strings
0    1                             NaN
1    2                             NaN
2    3                             NaN
3    4                             NaN
4    5                             NaN
5    6                             NaN
6    7                             NaN
7    8                             NaN
8    9  West Corporation global office
9   10                     West-Riding
10  11      University of West Florida
11  12                       Southwest
1 голос
/ 07 февраля 2020

Использование Regex.

Пример:

df = pd.DataFrame({'strings': ['south', 'north', 'east', 'west', 'west, east, south', 'west, west', 'north, north', 'north, south', 'West Corporation global office', 'West-Riding', 'University of West Florida', 'Southwest']})
df['R'] = df['strings'].replace(r"\b(south|north|east|west)\b,?", np.NAN, regex=True)
print(df)

Выход:

                           strings                               R
0                            south                             NaN
1                            north                             NaN
2                             east                             NaN
3                             west                             NaN
4                west, east, south                             NaN
5                       west, west                             NaN
6                     north, north                             NaN
7                     north, south                             NaN
8   West Corporation global office  West Corporation global office
9                      West-Riding                     West-Riding
10      University of West Florida      University of West Florida
11                       Southwest                       Southwest
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...