如何提取短语并将其附加到Pandas中的新列



我想从一列的末尾提取一个特定的短语,该短语具有-x, -y, -z字母,然后从该短语中提取这些-x, -y, -z字母并将它们附加到新列中。

例如food栏

I ate food -s
I ate food -c
I ate  food -v
I ate  food -u

我提取-s, -c, -v, -u并将其附加到新列监视

您可以尝试.str.extract的负面前瞻性

df['monitoring'] = df['food'].str.extract('food (-[scvu])(?![w])')

(?![w])表示"如果下一个字符在w"中,则匹配失败。

print(df)
food monitoring
0                        I ate food -s         -s
1                        I ate food -c         -c
2                       I ate  food -v         -v
3                       I ate  food -u         -u
4   I ate food -s today in the morning         -s
5   I ate food -S today in the morning        NaN
6                               BV-SCP        NaN
7                    diet -restrictive        NaN
8                    diet -sestrictive        NaN
9                 diet -v -restrictive        NaN
10                diet -sestrictive -s        NaN

最新更新