Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/313.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 从pandas中的数据帧列中搜索字符串模式_Python_Regex_Pandas - Fatal编程技术网

Python 从pandas中的数据帧列中搜索字符串模式

Python 从pandas中的数据帧列中搜索字符串模式,python,regex,pandas,Python,Regex,Pandas,继续我在堆栈中的最后一个问题 假设我有一个数据帧 name genre satya |ACTION|DRAMA|IC| satya |COMEDY|DRAMA|SOCIAL|MUSIC| abc |DRAMA|ACTION|BIOPIC| xyz |ACTION||ROMANCE|DARMA| def |ACTION|SPORT|COMEDY|IC| ghj |IC|ACTIONDRAMA|

继续我在堆栈中的最后一个问题

假设我有一个数据帧

 name         genre
 satya      |ACTION|DRAMA|IC|
 satya      |COMEDY|DRAMA|SOCIAL|MUSIC|
 abc        |DRAMA|ACTION|BIOPIC|
 xyz        |ACTION||ROMANCE|DARMA|
 def        |ACTION|SPORT|COMEDY|IC|
 ghj        |IC|ACTIONDRAMA|NOACTION|
从我上一个问题的答案来看,我可以搜索任何一种类型(前IC),如果它独立存在于类型栏中,而不是作为任何其他类型字符串值(音乐或传记电影)的一部分

现在我想知道动作和戏剧是否都出现在一个流派专栏中,但不一定是以特定的顺序出现,也不一定是字符串的一部分,而是单独出现

所以我需要输出行[1,3,4]中的行

 name         genre
 satya      |ACTION|DRAMA|IC|   # both adjacently present
 #row 2 will not come           # as only DRAMA present not ACTION
 abc        |DRAMA|ACTION|BIOPIC|   ### both adjacently present in diff. order
 xyz        |ACTION||ROMANCE|DARMA|   ### both present not adjacent
 ##row  5 should not present as DRAMA is not here
 ## row 6 should not come as both are not present individually(but present as one string part)
我试过类似的东西

 x = df[df['gen'].str.contains('\|ACTION\|DRAMA\|')]
 ### got only Row  1 (ACTION and DRAMA in adjacent and in order ACTION->DRAMA)
请有人建议可以在这里遵循/添加什么,以便我可以在这里获得所需的内容。

我认为您可以在和的两个条件下使用-
&

print df
    name                        genre
0  satya            |ACTION|DRAMA|IC|
1  satya  |COMEDY|DRAMA|SOCIAL|MUSIC|
2    abc        |DRAMA|ACTION|BIOPIC|
3    xyz      |ACTION||ROMANCE|DRAMA|
4    def     |ACTION|SPORT|COMEDY|IC|
5    ghj    |IC|ACTIONDRAMA|NOACTION|

print df['genre'].str.contains('\|ACTION\|') & df['genre'].str.contains('\|DRAMA\|') 
0     True
1    False
2     True
3     True
4    False
5    False
Name: genre, dtype: bool

print df[ df['genre'].str.contains('\|ACTION\|') & df['genre'].str.contains('\|DRAMA\|') ]
    name                    genre
0  satya        |ACTION|DRAMA|IC|
2    abc    |DRAMA|ACTION|BIOPIC|
3    xyz  |ACTION||ROMANCE|DRAMA|

我不是很确定这个答案,因为我这里没有编译器,但试着使用这个

(\\\动作\戏剧)。*?(\\动作\戏剧)


希望有帮助。

x=df[df['gen'].str.contains(r'(?=.*\bACTION\b)(?=.*\bDRAMA\b)]
如果有动作和戏剧,你想返回整行吗?或者只需检查它们是否在行中?@janleyu-想将行返回到另一个数据框。ghi呢?这真的是
动作剧
还是
动作剧
?@jezrael Works..可能是我有个奇怪的问题。。。有没有一种方法可以让这类事情变得动态(使用for循环或一些列表理解)…就像我想通过[x,y,z],那么对于x,y,z,结果应该是在一个基本数据帧上应用所有3个(如所讨论的动作和戏剧)。我的列表内容应该是可变长度的。我想这是你的评论问题-
np.logical\u和.reduce([X,Y,Z])
@jezrael-你能帮我一下吗