Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 使用Apply Map从DF中删除不需要的短语(Pandas,Python 3)_Python 3.x_Pandas - Fatal编程技术网

Python 3.x 使用Apply Map从DF中删除不需要的短语(Pandas,Python 3)

Python 3.x 使用Apply Map从DF中删除不需要的短语(Pandas,Python 3),python-3.x,pandas,Python 3.x,Pandas,现在,我有一个像这样的DF Words Words1 Little Red Red Apple Cracker Barrel Wood Grain Far Away Man Flat Rate Shipping 我想根据特定条件从DF中删除单元格,其中某些单词不能同时出现在同一短语中 例如:从以下DF中删除单元格,其中“Flat”与“Shipping”在同一短语中,“Far”与“Man”在同一短语中。我一直在玩类似的东西,但它不工

现在,我有一个像这样的DF

Words               Words1
Little Red         Red Apple
Cracker Barrel     Wood Grain
Far Away Man       Flat Rate Shipping
我想根据特定条件从DF中删除单元格,其中某些单词不能同时出现在同一短语中

例如:从以下DF中删除单元格,其中“Flat”与“Shipping”在同一短语中,“Far”与“Man”在同一短语中。我一直在玩类似的东西,但它不工作

Words.where(Words.applymap(lambda x: 'Flat' and 'Shipping' in x))
有什么想法吗?

你说的“移除单元格”是什么意思?我想你的意思是删除行,对吗?这将是一个<代码>应用程序>代码>,因为你会考虑事物的变化。

In [8]: def pred(x):
    c1 = ('Far' in x[0]) and ('Man' in x[0])
    c2 = ('Flat' in x[1]) and ('Shipping' in x[1])
   ...:     return c1 and c2

In [9]: df.apply(pred, axis=1)
Out[9]: 
0    False
1    False
2     True
dtype: bool
因此,您可以删除符合条件的行

In [10]: df[~df.apply(pred, axis=1)]
Out[10]: 
            Words      Words1
0      Little Red   Red Apple
1  Cracker Barrel  Wood Grain

str.match
方法与正则表达式一起使用可能会更快。

不幸的是,我正在使用Pandas,并且使用此方法会引发以下错误:indexer:('index out-bounds','occured at index 0')找出了它引发此错误的原因——不幸的是,这对我不起作用
DF=DF.applymap(如果str(x)else x.replace('$','')中的lambda x:x不是'$',''),则替换('$','')
DF=DF.applymap(如果str(x)else x.replace('%,'')中的lambda x:x不是'%','')