从 pandas 数据框中删除数字和用户的停用词
Remove numbers and user's stop words from pandas data frame
我想知道如何从数据集中删除一些变量,特别是数字和字符串列表。例如。
Test Num
0 bam 132
1 - 65
2 creation 47
3 MAN 32
4 41 831
... ... ...
460 Luchino 21
461 42 4126 7
462 finger 43
463 washing 1
我想要类似的东西
Test Num
0 bam 132
2 creation 47
... ... ...
460 Luchino 21
462 finger 43
463 washing 1
我在其中(手动)删除了 MAN(它应该包含在字符串列表中,如停用词)、-
和数字。
我试过使用 isdigit 但它不起作用,所以我确定我的代码中存在错误:
df['Text'].where(~df['Text'].str.isdigit())
以及我的停用词:
my_stop=['MAN','-']
df['Text'].apply(lambda lst: [x for x in lst if x in my_stop])
你好,你应该试试这个代码:
df[df['Text']!='MAN']
如果你想过滤你可以使用.loc
df = df.loc[~df.Text.str.isdigit() & ~df.Text.isin(['MAN']), :]
.where(cond, other)
returns 与自身形状相同的数据框或系列,但保留原始值 cond
为真,并用 other
替换错误的。
在 the docs
中阅读更多内容
我想知道如何从数据集中删除一些变量,特别是数字和字符串列表。例如。
Test Num
0 bam 132
1 - 65
2 creation 47
3 MAN 32
4 41 831
... ... ...
460 Luchino 21
461 42 4126 7
462 finger 43
463 washing 1
我想要类似的东西
Test Num
0 bam 132
2 creation 47
... ... ...
460 Luchino 21
462 finger 43
463 washing 1
我在其中(手动)删除了 MAN(它应该包含在字符串列表中,如停用词)、-
和数字。
我试过使用 isdigit 但它不起作用,所以我确定我的代码中存在错误:
df['Text'].where(~df['Text'].str.isdigit())
以及我的停用词:
my_stop=['MAN','-']
df['Text'].apply(lambda lst: [x for x in lst if x in my_stop])
你好,你应该试试这个代码:
df[df['Text']!='MAN']
如果你想过滤你可以使用.loc
df = df.loc[~df.Text.str.isdigit() & ~df.Text.isin(['MAN']), :]
.where(cond, other)
returns 与自身形状相同的数据框或系列,但保留原始值 cond
为真,并用 other
替换错误的。
在 the docs